0

ビューで「nested_simple_form」を使用しており、その中で「fields_for」を使用しています。また、データをシリアル化するために fields_for の下に fields_for があります。しかし、データのシリアル化に使用する 2 番目の 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 を使用しようとしましたが、税金のデータをフェッチしません。tax は serailize フィールドです。シリアル化フィールド (tax1、tax2、tax3、tax4、tax5) のデータベースに格納された直接データが必要です。

助けてください。

4

1 に答える 1

0

I resolved issue by following changes in second fields_for part:

 <% require 'ostruct' %>

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