无法在 Rails 中保存嵌套属性

发布于 2024-10-08 02:49:42 字数 1759 浏览 6 评论 0原文

Rails 新手。在我的应用程序中,我试图创建一个游戏数据库,每个游戏都有多个 MameControls 作为嵌套属性。用于输入 MameControls 的字段显示在“新”视图中,但不在“编辑”中,结果不会在“显示”中呈现,如果我 validate_presence_of :mameControls 它不会保存表单,并指出“Mame 控件不能为空”。在rails控制台中,Game.first(或last,或任何其他记录).mame_controls仅返回[]。据我所知,嵌套属性没有被保存,尽管我很确定我已经将所有内容设置为与 Railscast #196 中显示的类似。这是 Game 类:

class Game < ActiveRecord::Base
  has_many :mame_controls, :dependent => :destroy

  attr_accessible :name, :year, :company, :designer, :genre,
  :sb_info, :wiki_link, :arcade_history_link, :arcade_museum_link, 
  :caesar_link, :wildcard_link, :mame_controls

  accepts_nested_attributes_for :mame_controls, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true

  validates_presence_of :name, :year, :company, :genre, :sb_info, :mame_controls

end

mame_control.rb:

class MameControl < ActiveRecord::Base
  belongs_to :game

  attr_accessible :name, :game_action
end

输入或编辑 MameControls 的表单代码是这样的。

在 _form.html.erb 中:

  <% f.fields_for :mame_controls do |builder| %>
    <%= render "control_fields", :f => builder %>
<% end %>

_control_fields.html.erb:

<div class="field">
 <p>
     <%= f.label :name, "Mame Control Name" %><br />
  <%= f.text_field :name %><br />
  <%= f.label :game_action, "Game Action" %><br />
  <%= f.text_field :game_action %>
 </p>
</div>

在 games_controller.rb 中:

 def new
    @game = Game.new
    5.times do
       mame_control = @game.mame_controls.build
     end
  end

def create
    @game = Game.new(params[:game])
end

Rails newb here. In my app I'm trying to create a database of Games that each have multiple MameControls as nested attributes. The fields for entering MameControls show up in the New view but not in Edit, the results aren't rendered in Show, and if i validate_presence_of :mameControls it won't save the form, stating "Mame controls can't be blank." And in the rails console, Game.first(or last, or any other record).mame_controls returns only []. So as far as I can tell the nested attribute is not being saved, even though i'm pretty sure i've set everything up similarly to what is shown in Railscast #196. Here is the Game class:

class Game < ActiveRecord::Base
  has_many :mame_controls, :dependent => :destroy

  attr_accessible :name, :year, :company, :designer, :genre,
  :sb_info, :wiki_link, :arcade_history_link, :arcade_museum_link, 
  :caesar_link, :wildcard_link, :mame_controls

  accepts_nested_attributes_for :mame_controls, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true

  validates_presence_of :name, :year, :company, :genre, :sb_info, :mame_controls

end

mame_control.rb:

class MameControl < ActiveRecord::Base
  belongs_to :game

  attr_accessible :name, :game_action
end

The form code to enter in or edit MameControls is this.

in _form.html.erb:

  <% f.fields_for :mame_controls do |builder| %>
    <%= render "control_fields", :f => builder %>
<% end %>

_control_fields.html.erb:

<div class="field">
 <p>
     <%= f.label :name, "Mame Control Name" %><br />
  <%= f.text_field :name %><br />
  <%= f.label :game_action, "Game Action" %><br />
  <%= f.text_field :game_action %>
 </p>
</div>

in games_controller.rb:

 def new
    @game = Game.new
    5.times do
       mame_control = @game.mame_controls.build
     end
  end

def create
    @game = Game.new(params[:game])
end

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

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

发布评论

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

评论(3

风吹雨成花 2024-10-15 02:49:42

好吧,事实证明问题是我在 accepts_nested_attributes_for 中使用 reject_if 时出现的一些问题。首先,我正在检查错误的变量名(令人尴尬),并且我误解了该方法实际上在做什么,或者更确切地说没有做什么 - 如果先前填写的条目因空白而被拒绝(如在该 lambda 中),它不会' t 删除或清空数组中相应的索引值。它只是不会做任何事情。您必须将嵌套属性记录显式设置为 _destroy 才能销毁。我从理论的角度阅读了 accepts_nested_attributes_for,但我应该更好地理解它的选项。

Alright, turns out the problem was a couple things wrong with my use of reject_if in accepts_nested_attributes_for. First of all I was checking for the wrong variable name (embarrassing,) and I misunderstood what the method was actually doing, or rather not doing - if a previously filled entry is rejected for being blank (as in that lambda), it won't delete or null the corresponding index value in the array. It just won't do anything to it. You have to explicitly set the nested attribute record to _destroy to destroy. I had read up on accepts_nested_attributes_for from a theoretical standpoint but I should've understood its options better.

无声无音无过去 2024-10-15 02:49:42

尝试将 :mame_controls_attributes 添加到 attr_accessible,然后创建记录。然后,当您编辑记录时,编辑视图还应显示 mame_controls 的字段。

另外,为什么控件应该在显示操作中可见? (显示的目的是显示数据,而不是编辑数据)

Try adding :mame_controls_attributes to attr_accessible and then creating the record. Then, when you edit the record, the edit view should also show the fields for mame_controls.

Also, why should the controls be visible in show action ? (show is meant to display the data, not edit it)

囚你心 2024-10-15 02:49:42

我遇到了类似的问题。我让嵌套表单正确显示,但是当我提交表单时,它将父对象信息写入数据库,而不是子(嵌套)对象。

我的解决方案是将以下代码添加到 games_controller.rb 中的创建方法中(就像我使用您的示例一样)

def create
  @game = Game.new(params[:game])
  if @game.save
    @game.mame_control = MameControl.new(params[:mame_control])
    ...

,另请注意,我的父对象与子对象具有 has_one 关系,而不是 has_many 关系。

I was experiencing a similar problem. I got the nested form to display correctly, but when I submitted the form it wrote the parent object info to the database, but not the child (nested) object.

The solution for me was adding the following code to my (as if I were using your example) create method in games_controller.rb

def create
  @game = Game.new(params[:game])
  if @game.save
    @game.mame_control = MameControl.new(params[:mame_control])
    ...

And please also note that my parent object had a has_one relationship with the child, not has_many.

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