[英]Rails Polymorphic Nested Attribute
I have a polymorphic model and want to have a nested form use this model. 我有一个多态模型,想使用此模型使用嵌套形式。 I am getting no errors but the form is not displaying the nested field.
我没有收到任何错误,但表单未显示嵌套字段。 Here are my models and stripped down form:
这是我的模型并简化为表格:
class SeoMapping < ActiveRecord::Base
belongs_to :mappingtable, :polymorphic => true
attr_accessible :seo_url
validates :seo_url, :presence => true, :uniqueness => true
end
class Page < ActiveRecord::Base
has_one :seo_mappings, :as => :mappingtable, :dependent => :destroy
accepts_nested_attributes_for :seo_mappings
attr_accessible :content, :h1, :meta_description, :title, :seo_mappings_attributes
.........
end
<%= form_for(@page) do |f| %>
<% if @page.errors.any? %>
.......
<% end %>
<div class="field">
<%= f.fields_for :seo_mappings do |builder| %>
<%= builder.label :seo_url %><br />
<%= builder.text_field :seo_url %>
<% end %>
</div>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
.........
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I cant see why it does not display the fields_for elements. 我看不到为什么它不显示fields_for元素。 If I comment out accepts_nested_attributes_for the field then displays.
如果我注释掉该字段,则显示accepts_nested_attributes_。 Can you see where I am going wrong ?
你能看到我要去哪里错吗?
TY TY
Possibly a silly question but is this the create or edit action you're talking about? 可能是一个愚蠢的问题,但这是您正在谈论的创建或编辑操作吗? And if it's create, did you call @page.build_seo_mapping or something in the controller?
如果是创建的,您是否在控制器中调用了@ page.build_seo_mapping或其他内容?
Also (may be unrelated) if you use has_one, usually you want to use a singular noun, so has_one :seo_mapping instead of mappings. 同样,如果使用has_one(可能不相关),通常您希望使用单数名词,所以has_one:seo_mapping而不是映射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.