Frage

I need to put fields from journals and journal_entries into one row on a table, and have the ability to add and show many data entry rows in the same view. (ie a table of rows and using link_to_add_fields with accepts_nested_attributes to expand the rows in the table).

There has to be some kind of f.parent.text_field or f.object.parent.text_field?

I'm trying to do something like the following

<table>
#in a :pm namespace
<%= form_for [:pm, @lease] do |f| %>
  <%= f.fields_for :journal_entries do |journal_entries| %>
    <%= render "journal_entry_fields" , f: journal_entries %>
  <% end %>
  <%= link_to_add_fields "+ Add transactions", f, :journal_entries %>
<% end %>
</table>

_journal_entry_fields.html.erb

<fieldset>
  <tr>
    ## HERE IS WHAT I'M LOOKING FOR <<<<<<<<<<<!!>>>>>>>>>>>>>
    <td><%= f.parent.text_field :dated %></td>
    <td><%= f.parent.text_field :account_name %></td>
    <td><%= f.text_field :credit %></td>
    <td><%= f.text_field :notes %></td>        
  </tr>
</fieldset>

My Models

class Lease < ActiveRecord::Base
  has_many :journals, :order => [:dated, :id] #, :conditions => "journals.lease_id = id"
  has_many :journal_entries, :through => :journals  
  accepts_nested_attributes_for :journal_entries , :allow_destroy => true
  accepts_nested_attributes_for :journals ,  :allow_destroy => true    
end

class Journal < ActiveRecord::Base
  belongs_to :lease, :conditions =>  :lease_id != nil   
  has_many :journal_entries
  accepts_nested_attributes_for :journal_entries , :allow_destroy => true
end

class JournalEntry < ActiveRecord::Base
  belongs_to :journal
end

I'm using Rails 3.2.12 and ruby 1.9.3

I'm trying to see if this is a better solution than the problem faced on: rails link_to_add_fields not adding fields with has_many :through (with nested form inside)

I made a different thread because I think it's so drastically different.

Thanks, Phil

War es hilfreich?

Lösung

As per my understanding about your use-case you want to create journals and its entries in a single form of Lease. So, you can us the fields_for for both of them as below:

<table>
  #in a :pm namespace
  <%= form_for [:pm, @lease] do |f| %>
    <%= f.fields_for :journals do |journal| %>
      <%= render "journal_entry_fields" , f: journal %>
    <% end %>
  <%= link_to_add_fields "+ Add transactions", f, :journals %>
 <% end %>
</table>

_journal_entry_fields.html.erb

<fieldset>
  <tr>
    <td><%= f.text_field :dated %></td>
    <td><%= f.text_field :account_name %></td>
    <%= f.fields_for :journal_entries do |journal_entry| %>
      <td><%= journal_entry.text_field :credit %></td>
      <td><%= journal_entry.text_field :notes %></td>
    <% end %>   
  </tr>
</fieldset>

Though you need to initialise the journal entries every time a new record is added dynamically. I can't help you with this right now as I'm not on my PC.

Andere Tipps

Try this: http://railscasts.com/episodes/196-nested-model-form-revised

The RailsCasts model relations are similar to your model relations, although you would need to alter the HTML.

RailsCasts Models: Survey > Question > Answer

Your Models: Lease > Journal > JournalEntry

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top