0

我目前有一个嵌套模型User has_many Sales_Orders has_many Items

我可以正确创建带有嵌套项目的Sales_Order ,但是当我尝试“编辑” Sales_Order时,Sales_Order 信息可见但未显示项目。知道为什么这些不会显示吗?

[sales_orders_controller.rb]
...

def edit
  @sales_order = SalesOrder.find(params[:id])
end


[edit.html.erb]
<% provide(:title, "Edit SO") %>
<h1>Edit Sales Order</h1>

<div class="row">
<div class="span6 offset3">
    <%= form_for(@sales_order) do |f| %>
        <%= render 'shared/error_messages', object: f.object %>

        <div class="span3">
            <%= f.label :so, "SO#:" %>
            <%= f.text_field :so %>

            <%= f.label :customer, "Customer:" %>
            <%= f.text_field :customer %>

            <%= f.label :enter_date, "Date Entered:" %>
            <%= f.text_field :enter_date, value: date_formatter(@sales_order.enter_date) %>

            <%= f.label :request_date, "Request Date:"%>
            <%= f.text_field :request_date, value: date_formatter(@sales_order.request_date) %>

            <%= f.label :comments, "CS Comments:" %>
            <%= f.text_area :comments %>
        </div>

        <div class="span3">
            <% f.fields_for @sales_order.items do |builder| %>  
                <%= render 'item_fields', f: builder %>
            <% end %>
        </div>

        <%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
    <% end %>
</div>
</div>

[_item_fields.erb]
<fieldset>
<%= f.label :item_code, "Item Code:" %>
<%= f.text_field :item_code %>

<%= f.label :qty_in_kg, "Qty (kg):" %>
<%= f.text_field :qty_in_kg %>

<%= f.label :qc_comments, "Comments:" %>
<%= f.text_field :qc_comments %>

<%= link_to "remove", '#', class: "remove_fields" %>
</fieldset>
4

2 回答 2

1
<% f.fields_for @sales_order.items do |builder| %>  

应该

<%= f.fields_for @sales_order.items do |builder| %>  

如果没有,=您将在循环中构建输出,但从不渲染/打印返回值(生成的 HTML 输出)。

于 2013-04-29T15:16:07.037 回答
1

利用<%= f.fields_for @sales_order.items do |builder| %>

注意=符号。这意味着,“评估和嵌入”

于 2013-04-29T15:23:56.907 回答