Question

I am using an "nested_simple_form" in my view and in that i use "fields_for". I also have a fields_for under fields_for to work wih serailize data. But in second fields_for which use for serialize data it curently not getting values from database. I also tried to use OprnStruct but i also not workig for me.

models

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

view file (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%>

I tried to use OpenStruct but its not fetch data for taxes. taxes is a serailize field. I want direct data stored in data base for serialize fields(for tax1, tax2, tax3,tax4,tax5).

please help.

Was it helpful?

Solution

I resolved issue by following changes in second fields_for part:

 <% require 'ostruct' %>

<%= invoice.fields_for :taxes, OpenStruct.new(invoice.object.taxes)  do |tax| %> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top