【发布时间】:2009-05-04 01:04:02
【问题描述】:
我正在尝试为我的网站实现嵌套对象表单,使用 Ryan Daigle 的博客文章作为指南 (http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes)。由于某种原因,嵌套的表单字段不会出现在视图中。
class Instruction < ActiveRecord::Base
has_many :steps
accepts_nested_attributes_for :steps
end
class Step < ActiveRecord::Base
belongs_to :instruction
end
<% form_for @instruction do |instruction_form| %>
<%= instruction_form.error_messages %>
<p>
<%= instruction_form.label :title %><br />
<%= instruction_form.text_field :title %>
</p>
<p>
<%= instruction_form.label :difficulty %><br />
<%= instruction_form.text_field :difficulty %>
</p>
<% instruction_form.fields_for :steps do |step_form| %>
<%= step_form.label :explanation, 'Explanation: ' %>
<%= step_form.text_field :explanation %>
<% end %>
<p><%= instruction_form.submit "Submit" %></p>
<% end %>
当我将 instruction_form.fields_for :steps do |step_form| 更改为 instruction_form.fields_for :step do |step_form| 时,表单会呈现,但在提交时,我收到“未知属性:步骤”错误。
我正在做的似乎与教程相匹配。我应该检查什么?谢谢。
【问题讨论】:
标签: ruby-on-rails ruby nested-forms