出现嵌套表格字段的问题 [英] Problems with nested form fields showing up

查看:45
本文介绍了出现嵌套表格字段的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Ryan Daigle的博客文章作为指南来实现我的网站的嵌套对象表单(

I'm attempting to implement nested object forms for my site, using Ryan Daigle's blog post as a guide (http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes). For some reason, the nested form fields don't appear in the view.

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|时,将呈现表单,但提交后会出现未知属性:步骤"错误.

When I change instruction_form.fields_for :steps do |step_form| to instruction_form.fields_for :step do |step_form|, the form renders but upon submission, I get an 'unknown attribute: step' error.

我在做什么似乎与本教程相符.我应该检查什么?谢谢.

What I'm doing seems to match the tutorial. What should I check? Thanks.

推荐答案

控制器中发生了什么?我还没有阅读过该教程,现在似乎无法将其向上拉(向下?),但是您是否要在内存中建立一个对象以进行填充?

What is going on in your controller? I haven't read the tutorial yet, and can't seem to pull it up right now (down?) but are you building out an object in memory to fill out?

在您的控制器的新"操作中,确保您是

in your controller, in your "new" action, make sure that you are

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

这将实例化内存中的Step作为占位符",供您填写表单. . .至少这是我在使用accepts_nested_attributes_for时在自己的控制器中所做的事情,并且效果很好.

This will instantiate a Step in memory as a "placeholder" for your form to fill in . . . at least this is what I do in my own controller when using a accepts_nested_attributes_for, and it works great.

让我知道它是否有效,一旦我可以阅读本教程,我可能必须对其进行编辑

Let me know if it works, and once I can pull up the tutorial I may have to edit this

这篇关于出现嵌套表格字段的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆