2009-05-04 81 views
0

我试图为我的网站实现嵌套对象表单,使用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|的形式呈现,但在提交时,我得到一个“未知属性:步”的错误。

我在做什么似乎与教程相匹配。我应该检查什么?谢谢。

回答

2

控制器中发生了什么?我还没有看过这个教程,现在看起来似乎无法把它拉下来(down?),但是你是否在内存中构建了一个填充对象?

在你的控制器,在您的“新”行动,确保你是

@instruction = Instruction.new 
@instruction.steps.build 

这会在内存中实例化一个Step作为“占位符”为您的形式让我们填写。 。 。至少这是我在使用accepts_nested_attributes_for时在我自己的控制器中所做的,并且效果很好。

让我知道,如果它的工作原理,一旦我能拉起教程中,我可以编辑这个

+0

我知道我需要@instruction = Instruction.new不知道我需要做的@instruction .steps.build。 回顾本教程,我意识到我应该阅读'extras'部分。谢谢您的帮助! – sutee 2009-05-05 00:33:58