Greg Ma
2010-Feb-14 18:29 UTC
Can''t mass-assign these protected attributes: tag_attributes
Hi, I added a textarea to my edit student form, to be able to add tags to this student. I did the simple code below, and this is the error i get. I can''t see what i did wrong... #Error message Processing StudentsController#update (for 127.0.0.1 at 2010-02-14 10:13:22) [PUT] Parameters: {"commit"=>"Submit", "authenticity_token"=>"PNvzBi6Ro1XJg0c6CfgKsEw+wfg6DwZfnV2FUpRgqPs=", "id"=>"1", "student"=>{"full_name"=>"Gregory ", "cb_id"=>"68", "tag_attributes"=>"greg,toto", "description"=>"voila une jolie description"}} Student Columns (12.8ms) SHOW FIELDS FROM `students` Student Load (0.3ms) SELECT * FROM `students` WHERE (`students`.`id` = 1) WARNING: Can''t mass-assign these protected attributes: tag_attributes #Html form <% form_for @student do |f| %> <%= f.error_messages %> <p> <%= f.label :cb_id %><br /> <%= f.text_field :cb_id %> </p> <p> <%= f.label :full_name %><br /> <%= f.text_field :full_name %> </p> <p> <%= f.label :description%><br /> <%= f.text_area :description %> </p> <p> <%= text_area_tag "student[tag_attributes]" %> <p><%= f.submit "Submit" %></p> <% end %> #END #Student Class class Student < ActiveRecord::Base has_and_belongs_to_many :data_files has_and_belongs_to_many :tags attr_accessible :cb_id, :full_name, :description validates_uniqueness_of :cb_id def tag_attributes=(tags) splits = tags.split('','') splits.each do |tag| Tag.build(tag) end end end #END Thanks for your help Greg -- Posted via http://www.ruby-forum.com/. -- 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=en.
Rob Biedenharn
2010-Feb-14 18:46 UTC
Re: Can''t mass-assign these protected attributes: tag_attributes
On Feb 14, 2010, at 1:29 PM, Greg Ma wrote:> Hi, > I added a textarea to my edit student form, to be able to add tags to > this student. I did the simple code below, and this is the error i > get. > I can''t see what i did wrong... > > #Error message > Processing StudentsController#update (for 127.0.0.1 at 2010-02-14 > 10:13:22) [PUT] > Parameters: {"commit"=>"Submit", > "authenticity_token"=>"PNvzBi6Ro1XJg0c6CfgKsEw+wfg6DwZfnV2FUpRgqPs=", > "id"=>"1", "student"=>{"full_name"=>"Gregory ", "cb_id"=>"68", > "tag_attributes"=>"greg,toto", "description"=>"voila une jolie > description"}} > Student Columns (12.8ms) SHOW FIELDS FROM `students` > Student Load (0.3ms) SELECT * FROM `students` WHERE > (`students`.`id` > = 1) > WARNING: Can''t mass-assign these protected attributes: tag_attributes > > > #Html form > <% form_for @student do |f| %> > <%= f.error_messages %> > <p> > <%= f.label :cb_id %><br /> > <%= f.text_field :cb_id %> > </p> > <p> > <%= f.label :full_name %><br /> > <%= f.text_field :full_name %> > </p> > <p> > <%= f.label :description%><br /> > <%= f.text_area :description %> > </p> > <p> > <%= text_area_tag "student[tag_attributes]" %> > > <p><%= f.submit "Submit" %></p> > <% end %> > #END > > > #Student Class > class Student < ActiveRecord::Base > has_and_belongs_to_many :data_files > has_and_belongs_to_many :tags > > attr_accessible :cb_id, :full_name, :descriptionYou either need to add :tag_attributes to this list to make it accessible for mass updates... Or, in the controller code that you didn''t show, pluck the tag_attributes out: tag_attributes = params[:student].delete(''tag_attributes'') before you call @student.update_attributes(params[:student]) and add a call to: @student.tag_attributes = tag_attributes -Rob> > validates_uniqueness_of :cb_id > > def tag_attributes=(tags) > splits = tags.split('','') > splits.each do |tag| > Tag.build(tag) > end > end > end > #END > > Thanks for your help > Greg > -- > Posted via http://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > . >Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org -- 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=en.
Lasse Bunk
2010-Feb-14 18:46 UTC
Re: Can''t mass-assign these protected attributes: tag_attributes
I think you need to add :tag_attributes to attr_accessible in your model – would this fix the problem? Lasse 2010/2/14 Greg Ma <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>> Hi, > I added a textarea to my edit student form, to be able to add tags to > this student. I did the simple code below, and this is the error i get. > I can''t see what i did wrong... > > #Error message > Processing StudentsController#update (for 127.0.0.1 at 2010-02-14 > 10:13:22) [PUT] > Parameters: {"commit"=>"Submit", > "authenticity_token"=>"PNvzBi6Ro1XJg0c6CfgKsEw+wfg6DwZfnV2FUpRgqPs=", > "id"=>"1", "student"=>{"full_name"=>"Gregory ", "cb_id"=>"68", > "tag_attributes"=>"greg,toto", "description"=>"voila une jolie > description"}} > Student Columns (12.8ms) SHOW FIELDS FROM `students` > Student Load (0.3ms) SELECT * FROM `students` WHERE (`students`.`id` > = 1) > WARNING: Can''t mass-assign these protected attributes: tag_attributes > > > #Html form > <% form_for @student do |f| %> > <%= f.error_messages %> > <p> > <%= f.label :cb_id %><br /> > <%= f.text_field :cb_id %> > </p> > <p> > <%= f.label :full_name %><br /> > <%= f.text_field :full_name %> > </p> > <p> > <%= f.label :description%><br /> > <%= f.text_area :description %> > </p> > <p> > <%= text_area_tag "student[tag_attributes]" %> > > <p><%= f.submit "Submit" %></p> > <% end %> > #END > > > #Student Class > class Student < ActiveRecord::Base > has_and_belongs_to_many :data_files > has_and_belongs_to_many :tags > > attr_accessible :cb_id, :full_name, :description > > validates_uniqueness_of :cb_id > > def tag_attributes=(tags) > splits = tags.split('','') > splits.each do |tag| > Tag.build(tag) > end > end > end > #END > > Thanks for your help > Greg > -- > Posted via http://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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=en.
Greg Ma
2010-Feb-14 19:23 UTC
Re: Can''t mass-assign these protected attributes: tag_attributes
Lasse Bunk wrote:> I think you need to add :tag_attributes to attr_accessible in your model > – > would this fix the problem? > > Lasse > > 2010/2/14 Greg Ma <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>it works! Thanks -- Posted via http://www.ruby-forum.com/. -- 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=en.