João Pereira
2009-Nov-22 02:46 UTC
WARNING: Can''t mass-assign these protected attributes: active
Hi, I''m trying to understand this error: WARNING: Can''t mass-assign these protected attributes: active I had this error before when creating a record.I did not mark the attibutes posted from the form as attr_accessible, so when the following line tries to execute it throwed an Warning in the log, and the record was not saved. @user = User.new(params[:user]) I find out that I have to have the attributes that I want to sent from the form marked as attr_accessible. But now I''m not understanding the same error when I try to update a single parameter from the record. Let''s say I have this in my users controller: #Get from the params hash the activation key activation_key = params[:activation_key] #Get from the params Hash the user id user_id =params[:id] @user = User.find(user_id) @user.update_attributes!(:active=>1) and in the model I don''t have the param active :marked as attr_accessible. And the following warning messages is logged and the record is not updated WARNING: Can''t mass-assign these protected attributes: active Shouldn''t this work without having the :param active marked with attr_accessible? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=.
Frederick Cheung
2009-Nov-23 09:24 UTC
Re: WARNING: Can''t mass-assign these protected attributes: active
On Nov 22, 2:46 am, João Pereira <jonhy.p...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> user_id =params[:id] > @user = User.find(user_id) > @user.update_attributes!(:active=>1) > > and in the model I don''t have the param active :marked as attr_accessible. > And the following warning messages is logged and the record is not updated > > WARNING: Can''t mass-assign these protected attributes: active > > Shouldn''t this work without having the :param active marked with > attr_accessible?No - update_attributes, new etc. all behave the same way with respect to protected attributes. Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=.
Matt Jones
2009-Nov-23 16:18 UTC
Re: WARNING: Can''t mass-assign these protected attributes: active
On Nov 21, 9:46 pm, João Pereira <jonhy.p...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> @user.update_attributes!(:active=>1) > > and in the model I don''t have the param active :marked as attr_accessible. > And the following warning messages is logged and the record is not updated > > WARNING: Can''t mass-assign these protected attributes: active > > Shouldn''t this work without having the :param active marked with > attr_accessible?You''re still doing mass-assignment, just not with much "mass". The function you''re looking for is either @user.update_attribute(:active, true) or just @user.active = true followed by @user.save. --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=.