不能批量分配受保护的属性:tags_attributes? [英] Can't mass-assign protected attributes: tags_attributes?

查看:33
本文介绍了不能批量分配受保护的属性:tags_attributes?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照 Rails 指南为帖子创建标签:

tag.rb:

class 标签 

post.rb:

class Post 真的,:长度 =>{:最大=>30 },:唯一性 =>真的验证 :content, :presence =>真的,:唯一性 =>真的归属地:用户has_many :comments, :dependent =>:破坏has_many :votes, :as =>:votable, :dependent =>:破坏has_many :标签accepts_nested_attributes_for :tags, :allow_destroy =>:真的,:reject_if =>proc { |属性|attrs.all?{ |k, v|v.空白?} }结尾

views/posts/_form.html.erb:

<% @post.tags.build %><%= form_for(@post) 做 |post_form|%><%= 渲染 'shared/error_messages' %><div class="field"><%= post_form.label :title %><br/><%= post_form.text_field :title %>

<div class="field"><%= post_form.label :content %><br/><%= post_form.text_area :content %>

<h2>标签</h2><%=渲染:部分=>'标签/表单',:locals =>{:form =>post_form} %><div class="actions"><%= post_form.submit %>

<%结束%>

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

<% 除非 tag_form.object.nil?||tag_form.object.new_record?%><div class="field"><%= tag_form.label :_destroy, '删除:' %><%= tag_form.check_box :_destroy %>

<%结束%><%结束%>

但是当我尝试创建标签时出现此错误:

<块引用>

不能批量分配受保护的属性:tags_attributes Rails.root:/home/alex/rails/r7

应用程序跟踪 |框架跟踪 |完整跟踪app/controllers/posts_controller.rb:25:in `create' 请求

参数:

{"utf8"=>"✓","authenticity_token"=>"VF/qlfZ4Q5yvPY4VIbpFn65hoTAXdEa4fb4I1Ug4ETE=","post"=>{"title"=>"post number 5", "content"=>"post number 5 post5 号帖子 5", "tags_attributes"=>{"0"=>{"name"=>"food,饮料"}}},"提交"=>"创建帖子"}

有什么建议可以解决这个问题吗?

解决方案

Attr_accessible 指定不能批量分配属性.在这里,您还需要将 post_id 设为 attr_accessible.请参阅警告:无法批量分配受保护的属性

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?

解决方案

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

这篇关于不能批量分配受保护的属性:tags_attributes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆