如何确保在“编辑”期间显示所有嵌套属性

时间:2013-04-29 15:01:05

标签: ruby-on-rails ruby nested

我目前有一个嵌套模型用户 has_many Sales_Orders has_many 商品

我可以正确创建 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>

2 个答案:

答案 0 :(得分:1)

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

应该是

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

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

答案 1 :(得分:1)

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

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