# Can't mass-assign these protected attributes: tag\_attributes

**URL:** <https://www.ruby-forum.com/t/cant-mass-assign-these-protected-attributes-tag-attributes/183454>\
**Category:** Rails\
**Created:** [February 14, 2010, 7:29pm UTC](https://www.ruby-forum.com/t/cant-mass-assign-these-protected-attributes-tag-attributes/183454 "2010-02-14T19:29:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [February 14, 2010, 7:29pm UTC](https://www.ruby-forum.com/t/cant-mass-assign-these-protected-attributes-tag-attributes/183454/1 "2010-02-14T19:29:10Z")

</div>

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 %\>

\<%= f.label :cb\_id %\>  
 \<%= f.text\_field :cb\_id %\>

\<%= f.label :full\_name %\>  
 \<%= f.text\_field :full\_name %\>

\<%= f.label :description%\>  
 \<%= f.text\_area :description %\>

\<%= text\_area\_tag "student[tag\_attributes]" %\>

\<%= f.submit "Submit" %\>

\<% 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

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [February 14, 2010, 7:48pm UTC](https://www.ruby-forum.com/t/cant-mass-assign-these-protected-attributes-tag-attributes/183454/2 "2010-02-14T19:48:34Z")

</div>

On Feb 14, 2010, at 1:29 PM, Greg Ma wrote:

> “authenticity\_token”=\>“PNvzBi6Ro1XJg0c6CfgKsEw+wfg6DwZfnV2FUpRgqPs=”,  
> #Html form
> 
> #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

You 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

> #END  
> removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to removed\_email\_address@domain.invalid  
> .  
> For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en)  
> .

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [February 14, 2010, 8:23pm UTC](https://www.ruby-forum.com/t/cant-mass-assign-these-protected-attributes-tag-attributes/183454/3 "2010-02-14T20:23:02Z")

</div>

Lasse B. 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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

it works! Thanks

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [February 14, 2010, 7:48pm UTC](https://www.ruby-forum.com/t/cant-mass-assign-these-protected-attributes-tag-attributes/183454/4 "2010-02-14T19:48:34Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
