0

我在我的视图中使用“nested_simple_form”并且我使用“fields_for”。我在 fields_for 下还有一个 fields_for 来处理数据。但是在用于序列化数据的第二个 fields_for 中,它目前没有从数据库中获取值。我也尝试使用 OprnStruct 但我也不适合我。

楷模

invoice.rb

class Invoice < ActiveRecord::Base
  attr_accessible :invoice_line_items_attributes,:tax1,:tax2,:tax3,:tax4,:tax5
  attr_accessor :amount_due,:tax1,:tax2,:tax3,:tax4,:tax5,:tax1_value,:tax2_value,:tax3_value,:tax4_value,:tax5_value
  has_many :invoice_line_items,:dependent => :destroy

end

invoice_line_item.rb

class InvoiceLineItem < ActiveRecord::Base
belongs_to :invoice
    attr_accessible :invoice_id, :item_description:taxes,:tax1,:tax2,:tax3,:tax4,:tax5
    attr_accessor :tax1,:tax2,:tax3,:tax4,:tax5
    serialize :taxes, Hash

end

查看文件 (edit.html.erb)

<%= simple_nested_form_for(@invoice, :html => {:class => 'form-horizontal'}) do |f| %>
  <div class="form-inputs">
     some fields code here
   </div>

   <%= f.fields_for :invoice_line_items, :wrapper => false do |invoice| %>
      <tr>
       <td style="width:215px;" data-name="item-desc"><%= invoice.text_area :item_description, :as => :text,:cols => 5, :rows => 2',:class => "item_description_area"%></td></tr>
        <%= invoice.fields_for :taxes, @invoice.invoice_line_items.taxes  do |tax| %>   // this is not working.
                        <td class="tax1" style=""><%= tax.check_box :tax1,:class=> 'tax1', :label => false %></td>
                        <td class="tax2" style=""><%= tax.check_box :tax2,:class=> 'tax2', :label => false %></td>
                        <td class="tax3" style=""><%= tax.check_box :tax3,:class=> 'tax3', :label => false %></td>
                        <td class="tax4" style=""><%= tax.check_box :tax4,:class=> 'tax4', :label => false %></td>
                        <td class="tax5" style=""><%= tax.check_box :tax5,:class=> 'tax5', :label => false %></td>
                    <%end%>
   <%end%>

<%end%>

我尝试使用 OpenStruct,但它没有获取税收数据。税收是一个不稳定的领域。我希望将直接数据存储在数据库中以用于序列化字段(用于 tax1、tax2、tax3、tax4、tax5)。

请帮忙。

4

1 回答 1

0

我通过以下第二个 fields_for 部分的更改解决了问题:

 <% require 'ostruct' %>

<%= invoice.fields_for :taxes, OpenStruct.new(invoice.object.taxes)  do |tax| %> 
于 2013-10-31T13:48:22.480 回答