Strong parameters and attr_accessible are two different ways to add security for the Rails bulk assignment function . Strong parameters are a method which is set by the current version of Rails.
Bulk Assignment is a convenient abbreviated line in Rails that allows you to set many model properties in a single expression.
For example, imagine that you have @user that you want to update with data from a form view. Without mass assignment, you will have to write boring code as follows:
@user.first_name = params[:user][:first_name] @user.last_name = params[:user][:last_name] @user.phone_number = params[:user][:phone_number] ... @user.save
And for each form field and for inclusion.
For mass assignment, all of this code becomes one line:
@user.update(params[:user])
But it is full of security holes. . Since params contains any data sent by the browser, an attacker could add data to this view, which you did not expect. For example, they can add is_admin=1 to the parameters. If you have an is_admin database is_admin , then the mass assignment will simply allow the user to go to the administrator. Hop!
Here you will find strong options. With strong parameters, Rails raises ActiveModel::ForbiddenAttributesError if you try to do a naive update(params[:user]) . Instead, you need to clearly indicate what options you expect from the browser view, using the require and permit helpers, which provide strong options. Like this:
def user_params # Note that :is_admin is not permitted! params.require(:user).permit(:first_name, :last_name, :phone_number) end ... @user.update(user_params)
I canβt talk about supporting Rails, but I like Strong Parameters because it is flexible. If I have several actions in the user controller that expect different parameters, I can easily describe with permit parameters that should be allowed.
Or, if I have different user roles that are allowed to update different attributes, I can easily model these permissions. As mentioned in CVG Gate, you can even change these permissions at runtime, which is powerful.
In short, the flexibility of strong parameters is due to the fact that you can define this user_params method anywhere and as you like. You fully use the concepts of Ruby and OO to make them work the way you want.
What about attr_accessible ?
Without going into details (since this function is no longer supported by Rails): instead of using the permit method, you would do something similar using the attr_accessible macro in the model itself, for example:
class User < ActiveRecord::Base attr_accessible :first_name, :last_name, :phone_number ... end
So, for simple cases, it is very similar to Strong Parameters; you just define a list of attributes elsewhere.
However, since attr_accessible is closely related to the definition of a model class, you lose a lot of flexibility. What if you have two different controller actions that must perform bulk assignment for the same User model? Now you are stuck.
attr_accessor
The attr_accessor macro attr_accessor built into Ruby and has nothing to do with Rails, bulk assignment, or security. It just looks like a similar name. :)