Ruby on rails Can';t mass assign protected attributes:tags\u attributes?

Ruby on rails Can';t mass assign protected attributes:tags\u attributes?,ruby-on-rails,Ruby On Rails,我试图通过以下方式为帖子创建标签: tag.rb: class Tag < ActiveRecord::Base attr_accessible :name belongs_to :post end class Post < ActiveRecord::Base attr_accessible :title, :content, :tags validates :title, :presence => true,

我试图通过以下方式为帖子创建标签:

tag.rb:

class Tag < ActiveRecord::Base
  attr_accessible :name

  belongs_to :post
end
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
<% @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 %>
<%= 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 %>
class标记
post.rb:

class Tag < ActiveRecord::Base
  attr_accessible :name

  belongs_to :post
end
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
<% @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 %>
<%= 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 %>
class Posttrue,
:length=>{:max=>30},
:唯一性=>true
验证:content,:presence=>true,
:唯一性=>true
属于:用户
有很多:注释,:依赖=>:销毁
有很多:投票,:as=>:votable,:dependent=>:destroy
有很多:标签
接受:标记的\u嵌套的\u属性,\u,:允许\u销毁=>:true,
:如果=>proc{attrs}attrs.all{{k,v{v.blank?}
结束
views/posts/\u form.html.erb:

class Tag < ActiveRecord::Base
  attr_accessible :name

  belongs_to :post
end
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
<% @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 %>
<%= 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 %>



标签 “标签/表格”, :locals=>{:form=>post_form}%>
视图/标签/\u form.html.erb:

class Tag < ActiveRecord::Base
  attr_accessible :name

  belongs_to :post
end
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
<% @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 %>
<%= 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 %>

但当我尝试创建标记时,会出现以下错误:

无法批量分配受保护的属性:标记\u属性Rails.root: /home/alex/rails/r7

应用程序跟踪|框架跟踪|完整跟踪 app/controllers/posts_controller.rb:25:在“创建”请求中

参数:

{“utf8”=>“✓", “真实性令牌”=>“VF/qlfZ4Q5yvPY4VIbpFn65hoTAXdEa4fb4I1Ug4ETE=“, “帖子”=>{“标题”=>“第5号帖子”,“内容”=>“第5号帖子” 第5条第5条,“标签属性”=>{“0”=>{“名称”=>“食品, 饮料“}}”,提交“=>“创建帖子”}


任何解决此问题的建议?

Attr\u accessible指定您不能批量分配属性。 在这里,您还需要将post_id设置为attr_可访问。
请参考

只需放置:tags\u属性而不是:tags。请参考下面的内容。这将解决我面临的问题

class Post < ActiveRecord::Base
 attr_accessible :title, :content, :tags_attributes
end
class Post
这对我很有用:

class Post < ActiveRecord::Base
  attr_accessible :name, :title, :content, :tags_attributes
end
class Post
这对我也很有用:

class Post < ActiveRecord::Base
  attr_accessible :title, :content, :tags_attributes
end
class Post
这允许您通过Post访问标记属性