未设置的字段出现批量分配错误

发布于 2024-12-01 04:15:47 字数 2968 浏览 0 评论 0原文

我在 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:

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

I've got a nested form in Rails 3 as follows that works just fine during creation. In the edit stage, I'm getting WARNING: Can't mass-assign protected attributes: type. The form shouldn't be trying to set "type" and isn't doing so as far as I can tell from the parameters.

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

All of those nomination classes are subclasses of this base class:

class TagNomination < ActiveRecord::Base
  belongs_to :tag_set_nomination

  ....
end

And here's the relevant bit of my form:

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

And some hopefully relevant bits from the log:

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

And then I get dumped back to edit with no errors in the page. D:

Any ideas welcome! I am baffled.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贱人配狗天长地久 2024-12-08 04:15:47

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

捂脸

Okay, I figured it out, sigh. It had zero to do with any of the code I pasted. I had a before_save callback that was setting a value to either true or false -- and of course, when it returned false, the before_save callback died and therefore the save got rolled back.

facepalm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文