Skip to content

Commit f8e6420

Browse files
benschwarzquamen
authored andcommitted
Formatted the README to my liking and swapped out allow_assignment for the new allow_params API. Example of single param usage
Signed-off-by: Gareth Townsend <[email protected]>
1 parent 921082e commit f8e6420

File tree

1 file changed

+36
-42
lines changed

1 file changed

+36
-42
lines changed

README.markdown

+36-42
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
Bouncer
2-
=======
1+
# Bouncer
32

43
Your rails app is like a club. Everyone wants in. Most users are fine, but every now and again someone wants to cause trouble.
54

@@ -8,73 +7,68 @@ You need a bouncer to keep the riff raff out.
87
Bouncer allows you to filter the params passed into your controller so that you can safely manage mass assignment at the controller level.
98

109

11-
Example
12-
=======
10+
## Example
1311

1412
Take a user model with the following attributes: login, password, admin.
1513

1614
admin is a boolean that signifies whether the user has the ability to perform admin tasks.
1715

1816
You probably use the following code throughout your application in controllers.
1917

20-
<pre>
21-
<code>
22-
@user = User.new(params[:user])
23-
</code>
24-
</pre>
18+
@user = User.new(params[:user])
19+
2520

2621
This code is problematic. Should a user decide to fiddle with your parameters and pass in admin=true you could have a bit of a problem on your hands.
2722

2823
You need a bouncer with a door list.
2924

30-
<pre>
31-
<code>
32-
class UsersController < ApplicationController
33-
allow_assignment :user => [ :login, :password ]
34-
end
35-
</code>
36-
</pre>
25+
class UsersController < ApplicationController
26+
allow_params :user => [ :login, :password ]
27+
end
3728

38-
Now you can safely use <code> User.new(params[:user])</code> in your controller to mass assign only the attributes you've said are safe.
29+
Now you can safely use `User.new(params[:user])` in your controller to mass assign only the attributes you've said are safe.
3930

40-
You should use allow_assignment in every controller within your application. By default bouncer will strip everything from the params hash that isn't required by rails to operate.
31+
You should use allow_params in every controller within your application. By default bouncer will strip everything from the params hash that isn't required by rails to operate.
4132

42-
Why Not Use attr_accessible?
43-
==========================
33+
# Why Not Use attr_accessible?
4434

45-
attr_accessible is great. But it's handled at the model level.
35+
`attr_accessible` is great. But it's handled at the model level.
4636

4737
You might want to allow assignment of the admin attribute from an admin specific user interface.
4838

4939
You end up with code like this:
5040

51-
<pre>
52-
<code>
53-
class AdminUsersController < ApplicationController
41+
class AdminUsersController < ApplicationController
5442

55-
...
56-
@user = User.new(params[:user])
57-
@user.admin = params[:user][:admin]
58-
...
43+
...
44+
@user = User.new(params[:user])
45+
@user.admin = params[:user][:admin]
46+
...
5947

60-
end
61-
</code>
62-
</pre>
48+
end
49+
6350

6451
The following is much nicer:
6552

66-
<pre>
67-
<code>
68-
class UsersController < ApplicationController
69-
allow_assignment :user => [ :login, :password ]
70-
end
53+
class UsersController < ApplicationController
54+
allow_params :user => [ :login, :password ]
55+
end
56+
57+
class AdminUsersController < ApplicationController
58+
allow_params :user => [ :login, :password, :admin ]
59+
end
60+
61+
You can also post single params like `:id` for things like activation tokens and pagination keys.
62+
63+
class ActivationsController < ApplicationController
64+
allow_params :token
65+
66+
def create
67+
@user = User.find_by_activation_token(params[:token])
68+
end
69+
end
7170

72-
class AdminUsersController < ApplicationController
73-
allow_assignment :user => [ :login, :password, :admin ]
74-
end
75-
</code>
76-
</pre>
7771

7872
Copyright (c) 2009 Gareth Townsend, released under the MIT license
7973

80-
Thanks to Josh Bassett for helping nut out <code>self.request.env['rack.routing_args'].keys</code> and other refactorings.
74+
Thanks to Josh Bassett for helping nut out `self.request.env['rack.routing_args'].keys` and other refactorings.

0 commit comments

Comments
 (0)