嵌套表单不适用于 has_many
我有一个模型订单,
class Order < ActiveRecord::Base
has_many :order_details, :class_name => "OrderDetail"
accepts_nested_attributes_for :order_details
end
在我看来,我尝试构建一个嵌套表单,
<%= semantic_form_for @order do |f| %>
<%= f.inputs :name => "Detail", :for => :order_detail do |od| %>
<%= od.input :shoe_id, :collection => Shoe.all.map{|s|[s.article_number,s.id]} %>
<%= od.input :size_id, :collection => Size.all.map{|s|[s.number,s.id]} %>
<%= od.input :color_id, :collection => Color.all.map{|c|[c.name,c.id]} %>
<%= od.input :quantity %>
<%- end -%>
<%- end -%>
它有效。但使用则
:for => :order_details
不然。它什么也不呈现。
==解决了!!
我找到了解决方案。
@order.order_details 是空的,因此不会呈现nested_form。
在控制器中写入:
@order.order_details.build
I've got a model Order
class Order < ActiveRecord::Base
has_many :order_details, :class_name => "OrderDetail"
accepts_nested_attributes_for :order_details
end
When in my view I try to build a nested form
<%= semantic_form_for @order do |f| %>
<%= f.inputs :name => "Detail", :for => :order_detail do |od| %>
<%= od.input :shoe_id, :collection => Shoe.all.map{|s|[s.article_number,s.id]} %>
<%= od.input :size_id, :collection => Size.all.map{|s|[s.number,s.id]} %>
<%= od.input :color_id, :collection => Color.all.map{|c|[c.name,c.id]} %>
<%= od.input :quantity %>
<%- end -%>
<%- end -%>
It works. But using
:for => :order_details
does not. It renders nothing.
== SOLVED!!
I found the solution.
@order.order_details is emtpy so no nested_form is rendered.
writting in the controller:
@order.order_details.build
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
fields_for 用于渲染嵌套属性,我认为以下应该有效
<%- end -%>
我们可以使用 fields_for 助手创建多个嵌套记录
请参阅 Railscast
http://railscasts.com/episodes/196-nested- model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2
fields_for is used to rendering nested attributes,i think following should work
<%- end -%>
and we can create multiple nested record with fields_for helper
see rails cast
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2
我找到了解决方案。
在控制器中写入:
将使格式渲染正确
I found the solution.
writting in the controller:
will make formtastic render correctly
我找到了解决方案。
@order.order_details 是空的,因此不会呈现nested_form。
在控制器中写入:
I found the solution.
@order.order_details is emtpy so no nested_form is rendered.
writting in the controller: