Question

I've added an "avatar" to my user model (Devise) and am getting the error Can't mass-assign protected attributes: avatar

In my user model I have added:

attr_accessible :avatar_uid, :avatar_name
image_accessor :avatar

And the form (simple_form) has :html => { :method => :put, :multipart => true } and fields:

<%= f.file_field :avatar %>
<%= f.hidden_field :retained_avatar %>
<%= f.check_box :remove_avatar %>

Adding :avatar to attr_accessible solves the mass assignment error but the fields just don't get saved in the user table.

Was it helpful?

Solution

You need to add avatar in attr_accessible list as well in order to mass-assign it.

So just replace attr_accessible list by,

 attr_accessible :avatar_uid, :avatar_name, :avatar

image_accessor is provided by dragon-fly gem which provide reader/writer for listed attribute like attr_accessor.

check this out this thread to know more differnce between attr_accessible and attr_accessor

"WARNING: Can't mass-assign protected attributes"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top