I have encountered a problem in my application and realized that I could fix it by setting :without_protection => true when creating a model, e.g.:
Model.new(params[:model], :without_protection => true).
What exactly is rails protecting the models from? Thanks!
It’s protection against unintended mass assignment.
The problem with the code you shown is that users can alter the form and change attributes you don’t want them to change, like hashed passwords on users or a published status on posts.
You can use
attr_protectedandattr_accessibleon models to protect attributes on your models to be overridden. When an attribute is protected than the value fromparamswill be ignored (a notice will appear in your log).Before Rails 3.1, that was it. There was no way to configure it afterwards. Now, with Rails 3.1, you can assign roles:
And specify it when doing mass updates (
neworupdate_attributes):Using
:without_protection, will make every attribute free to be mass assigned and should be used VERY sparingly. Never use when you’re passing in user data. You might use it indb/seeds.rbfor example.