形式与HAS_ONE协会在Rails 3中不工作嵌套属性 [英] Form with nested attributes with a has_one association not working in Rails 3

查看:117
本文介绍了形式与HAS_ONE协会在Rails 3中不工作嵌套属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我特林两个模型在使用accepts_nested_attributes_for一个HAS_ONE协会设置的值,但没有在fields_for正在对视图中显示。

I'm tring to set values for two models that have a has_one association using accepts_nested_attributes_for, but nothing in the fields_for is being shown on the view.

我已确认在同一code工作on Rails的2.X,和正常工作时,其一个的has_many。
低于code。

I have confirmed the same code works on Rails 2.x, and it works fine when its a has_many. Code below.

示范

class Parent < ActiveRecord::Base

  has_one :child
  accepts_nested_attributes_for :child
end

class Child < ActiveRecord::Base

  belongs_to :parent
end

控制器

def new
    @parent = Parent.new
    @parent.build_child
end

查看

<%= form_for @parent do |f| %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>

  <% f.fields_for :child do |builder| %>

    <%= builder.label :childname %>
    <%= builder.text_field :childname %>
  <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

..和复制的,看着它之后,这是一个可怕的类名。

..And after copying that and looking at it, that's a horrible class name.

推荐答案

在轨道3,你应该使用(请注意&LT等号;%=

In rails 3 you should use (notice the equal sign in <%=):

<%= f.fields_for [...]

而不是:

<% f.fields_for

同样还有的form_for

same goes with form_for

这篇关于形式与HAS_ONE协会在Rails 3中不工作嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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