【问题标题】:getting mass-assignment error for field that isn't being set为未设置的字段获取质量分配错误
【发布时间】:2011-08-23 03:20:14
【问题描述】:

我在 Rails 3 中有一个嵌套表单,如下所示,它在创建过程中工作得很好。在编辑阶段,我收到警告:无法批量分配受保护的属性:类型。表单不应该尝试设置“类型”,并且据我从参数中可以看出并没有这样做。

class TagSetNomination < ActiveRecord::Base
  belongs_to :pseud
  belongs_to :owned_tag_set

  has_many :fandom_nominations, :dependent => :destroy
  has_many :character_nominations, :dependent => :destroy
  has_many :relationship_nominations, :dependent => :destroy
  has_many :freeform_nominations, :dependent => :destroy

  accepts_nested_attributes_for :fandom_nominations, :character_nominations, :relationship_nominations, :freeform_nominations, {
    :allow_destroy => true,
    :reject_if => proc { |attrs| attrs[:tagname].blank? }
  }
....
end

所有这些提名类都是这个基类的子类:

class TagNomination < ActiveRecord::Base
  belongs_to :tag_set_nomination

  ....
end

这是我的表单的相关部分:

<%= error_messages_for :tag_set_nomination %>

<%= form_for(@tag_set_nomination, :url => (@tag_set_nomination.new_record? ? tag_set_nominations_path(@tag_set) : tag_set_nomination_path(@tag_set, @tag_set_nomination)), :html => {:method => (@tag_set_nomination.new_record? ? :post : :put)}) do |f| %>

  <h4><%= ts("Tag Nominations") %></h4>
  <fieldset class="tagset">
    <dl>
      <% @tag_set_nomination.character_nominations.each_with_index do |character_nomination, index| %>
        <%= f.fields_for :character_nominations, character_nomination do |nom_form| %>
          <%= render 'tag_nominations', :nom_form => nom_form, :tag_type => 'character', :tag_nominations_counter => index %>
        <% end %>
      <% end %>
    </dl>
  </fieldset>

....
<% end %>

以及日志中一些希望相关的部分:

Started POST "/tag_sets/1/nominations/3" for 68.175.83.208 at 2011-08-23 02:59:08 +0000
Parameters: { ... "tag_set_nomination"=>{"character_nominations_attributes"=>{"0"=>{"tagname"=>"Sam", "parent_tagname"=>"", "tagnotes"=>"", "id"=>"12"}, "1"=>{"tagname"=>"Dean", "parent_tagname"=>"", "tagnotes"=>"", "id"=>"13"}, "2"=>{"tagname"=>"Yarbld", "parent_tagname"=>"Supernatural", "tagnotes"=>"some notes", "id"=>"16"}} ... }

SQL (0.1ms)  SELECT COUNT(*) FROM `tag_nominations` WHERE `tag_nominations`.`type` = 'CharacterNomination' AND (`tag_nominations`.tag_set_nomination_id = 3)
...
SQL (0.2ms)  ROLLBACK
Pseud Load (0.2ms)  SELECT `pseuds`.* FROM `pseuds` WHERE (`pseuds`.user_id = 8)
CharacterNomination Load (0.3ms)  SELECT `tag_nominations`.* FROM `tag_nominations` WHERE `tag_nominations`.`type` = 'CharacterNomination' AND (`tag_nominations`.tag_set_nomination_id = 3)
WARNING: Can't mass-assign protected attributes: type
WARNING: Can't mass-assign protected attributes: type
WARNING: Can't mass-assign protected attributes: type

然后我被转储回去编辑,页面中没有错误。 D:

欢迎任何想法!我很困惑。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 forms nested-forms nested-attributes


    【解决方案1】:

    你能在你的模型中创建type 字段attr_accessible 吗?

    我找到了 2 个可能有帮助的链接:

    http://somethinglearned.com/articles/2006/05/24/best-practices-a-strong-case-for-attr_accessible-part-2

    http://api.rubyonrails.org/classes/ActiveRecord/MultiparameterAssignmentErrors.html

    希望这会有所帮助!

    【讨论】:

    • 感谢 Rushabh,但我实际上并不想通过表单设置类型——类型用于 Rails STI,在这种情况下应该自动处理。正如您在参数中看到的那样,实际上并未设置类型。
    【解决方案2】:

    好吧,我想通了,叹息。它与我粘贴的任何代码都为零。我有一个 before_save 回调将值设置为 true 或 false - 当然,当它返回 false 时,before_save 回调终止,因此保存被回滚。

    捂脸

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多