嵌套表单不适用于 has_many

发布于 2024-12-04 05:21:56 字数 957 浏览 2 评论 0原文

我有一个模型订单,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

颜漓半夏 2024-12-11 05:21:56

fields_for 用于渲染嵌套属性,我认为以下应该有效

<%= semantic_form_for @order do |f| %>
   <%= f.fields_for :order_details 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 -%>

我们可以使用 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

<%= semantic_form_for @order do |f| %>
   <%= f.fields_for :order_details 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 -%>

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

心头的小情儿 2024-12-11 05:21:56

我找到了解决方案。

@order.order_details is emtpy so no nested_form is rendered.

在控制器中写入:

@order.order_details.build

将使格式渲染正确

I found the solution.

@order.order_details is emtpy so no nested_form is rendered.

writting in the controller:

@order.order_details.build

will make formtastic render correctly

那一片橙海, 2024-12-11 05:21:56

我找到了解决方案。

@order.order_details 是空的,因此不会呈现nested_form。

在控制器中写入:

@order.order_details.build

I found the solution.

@order.order_details is emtpy so no nested_form is rendered.

writting in the controller:

@order.order_details.build
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文