嵌套属性的fields_for不返回任何内容 [英] fields_for for nested attribute returns nothing
问题描述
我正在尝试在Rails 3.0.3中创建一个嵌套的模型表单.这是我的模特:
I'm trying to create a nested model form in Rails 3.0.3. Here are my models:
class Bird < ActiveRecord::Base
has_one :taxon, :as => :organism
accepts_nested_attributes_for :taxon
end
class Taxon < ActiveRecord::Base
belongs_to :organism, :polymorphic => true
end
这是控制器方法:
def new
@bird = Bird.new
@bird.build_taxon
end
这是表格:
New Bird
<% form_for @bird do |f| %>
<p>
<%= f.label :wingspan %>
<%= f.text_field :wingspan %>
</p>
<p>
<%= f.label :body_length %>
<%= f.text_field :body_length %>
</p>
<% f.fields_for :taxon do |builder| %>
<%= builder.label :common_name %>
<%= builder.text_field :common_name %>
<%= builder.label :genus_name %>
<%= builder.text_field :genus_name %>
<%= builder.label :species_name %>
<%= builder.text_field :species_name %>
<% end %>
<%= f.submit %>
<% end %>
当我运行新方法时,分类单元的字段不会显示.在html源代码中没有任何提示.我听说如果嵌套模型为nil(即如果我忘记了在controller方法中构建模型),则可能会发生这种情况,但是它就在那里.我在视图中添加了一些条件代码只是为了确保.
When I run the new method, The fields for taxon don't show up. There's no hint of them in the html source. I've heard that this can happen if the nested model is nil (i.e. if I had forgotten to build it in the controller method), but it's there. I added some conditional code in the view just to make sure.
那么,谁能让我在这里sm我的额头?我想念什么?
So, who will make me smack my forehead here? What am I missing?
谢谢!
推荐答案
您是否正在使用Rails 3?如果是这样,应该是:
Are you using Rails 3? If so it should be:
<%= form_for @bird do |f| %>
和
<%= f.fields_for :taxon do |builder| %>
注意等号.
这篇关于嵌套属性的fields_for不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!