我正在尝试在 Rails 3.0.3 中创建嵌套模型表单。这是我的模型:
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 源代码中没有任何提示。我听说如果嵌套模型为零(即如果我忘记在控制器方法中构建它),就会发生这种情况,但它就在那里。我在视图中添加了一些条件代码只是为了确保。
那么,谁会让我在这里捶我的额头呢?我错过了什么?
谢谢!