Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't mass-assign protected attributes: tags_attributes?

I'm trying to create tags for posts by following the Rails Guide:

tag.rb:

class Tag < ActiveRecord::Base
  attr_accessible :name

  belongs_to :post
end

post.rb:

class Post < ActiveRecord::Base
  attr_accessible :title, :content, :tags

  validates :title,   :presence => true,
                      :length   => { :maximum => 30 },
                      :uniqueness => true
  validates :content, :presence => true,
                      :uniqueness => true

  belongs_to :user

  has_many :comments, :dependent => :destroy
  has_many :votes, :as => :votable, :dependent => :destroy 
  has_many :tags

  accepts_nested_attributes_for :tags, :allow_destroy => :true,
    :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end

views/posts/_form.html.erb:

<% @post.tags.build %>
<%= form_for(@post) do |post_form| %>
  <%= render 'shared/error_messages' %>
  <div class="field">
    <%= post_form.label :title %><br />
    <%= post_form.text_field :title %>
  </div>
  <div class="field">
    <%= post_form.label :content %><br />
    <%= post_form.text_area :content %>
  </div>
  <h2>Tags</h2>
  <%= render :partial => 'tags/form',
             :locals => {:form => post_form} %>
  <div class="actions">
    <%= post_form.submit %>
  </div>
<% end %>

views/tags/_form.html.erb:

<%= form.fields_for :tags do |tag_form| %>
  <div class="field">
    <%= tag_form.label :name, 'Tag:' %>
    <%= tag_form.text_field :name %>
  </div>
  <% unless tag_form.object.nil? || tag_form.object.new_record? %>
    <div class="field">
      <%= tag_form.label :_destroy, 'Remove:' %>
      <%= tag_form.check_box :_destroy %>
    </div>
  <% end %>
<% end %>

But I get this error when I try to create tags:

Can't mass-assign protected attributes: tags_attributes Rails.root: /home/alex/rails/r7

Application Trace | Framework Trace | Full Trace app/controllers/posts_controller.rb:25:in `create' Request

Parameters:

{"utf8"=>"✓", "authenticity_token"=>"VF/qlfZ4Q5yvPY4VIbpFn65hoTAXdEa4fb4I1Ug4ETE=", "post"=>{"title"=>"post number 5", "content"=>"post number 5 post number 5 post number 5", "tags_attributes"=>{"0"=>{"name"=>"food, drinks"}}}, "commit"=>"Create Post"}

Any suggestions to fix this?

like image 876
alexchenco Avatar asked Feb 25 '12 06:02

alexchenco


4 Answers

Just put :tags_attributes instead of :tags . Please refer below . This will solve the problem as same faced by me

class Post < ActiveRecord::Base
 attr_accessible :title, :content, :tags_attributes
end
like image 106
Vivek Parihar Avatar answered Sep 22 '22 17:09

Vivek Parihar


Attr_accessible specifies that you can not mass assign attributes. Here, you need to make post_id as attr_accessible as well. Please refer WARNING: Can't mass-assign protected attributes

like image 27
asitmoharna Avatar answered Sep 19 '22 17:09

asitmoharna


This worked for me:

class Post < ActiveRecord::Base
  attr_accessible :name, :title, :content, :tags_attributes
end
like image 27
Bruno Avatar answered Sep 21 '22 17:09

Bruno


This worked for me too:

class Post < ActiveRecord::Base
  attr_accessible :title, :content, :tags_attributes
end

This allow you access tags attributes through Post.

like image 28
Duany Dreyton Avatar answered Sep 21 '22 17:09

Duany Dreyton