开发者

Rails 3 access nested attributes in nested form

开发者 https://www.devze.com 2023-04-12 08:33 出处:网络
I have an Invoice, that contains LineItems that belong to item Items. Invoice client_id LineItem invoice_id

I have an Invoice, that contains LineItems that belong to item Items.

Invoice

client_id

LineItem

invoice_id

item_id

quantity

cost (because cost could be edited per invoice if needed)

Item

name

cost

The problem开发者_如何学C is, in the Edit form. I want to display the cost and description but I can't.

_form.html.erb

<%= form_for(@invoice) do |f| %>  
  <div class="field">
    <%= f.label :organization_id, 'Client' %>
    <%= f.collection_select :client_id, @clients, :id, :name, :prompt => 'Select' %>
  </div>

<table id="invoice_items">
  <tr><th>Item</th><th>Description</th><th>Unit Cost</th><th>Qty</th></tr>
  <%= f.fields_for :line_items do |builder| %>
    <%= render 'line_item_fields', :f => builder %>
  <% end %>
  </table>
  <div class="actions">
    <%= f.submit 'Save', :disable_with => "Processing..." %>
  </div>
<% end %>

_line_items_fields.html.erb

<tr>
  <td><%= f.collection_select :item_id, @items, :id, :name, :prompt => '' %></td>
  <td>Description here</td>
  <td><%= f.text_field :cost, :class => 'cost' %></td>
  <td><%= f.text_field :quantity, :class => 'qty' %></td>
</tr>

How could I display the cost and description in a div in _line_items_fields.html.erb? I will need to display the cost in a div because later I will need to use it for jQuery to grab that value to do some calculation.


I guess you're looking for object.

Something like:

<%= f.object.cost unless f.object.cost.blank? %>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号