2014-09-23 20 views
0

我想在簡單的表單上使用simple_fields_for獲取一些空行,但只顯示一行/記錄。這是下面的代碼,模型,控制器和視圖。Simple_Fields_For:表單上的多個空行

class Journal < ActiveRecord::Base 
    has_many :journal_lines 
end 

class JournalLine < ActiveRecord::Base 
    belongs_to :journal 
end 


class JournalController < ApplicationController 
    def new 
     @organisation = Organisation.find(params[:organisation_id]) 
     @journal = Journal.new 
     @journal.journal_lines = Array.new(5, JournalLine.new) 
    end 
... 


<%= simple_form_for @journal, :url => {:controller => 'journal', :action => 'create', :id => @organisation.id}, :method => :post do |f| %> 
    <%= f.input :transaction_date, as: :string, input_html: {class: 'datepicker'} %> 
    <%= f.input :reference %> 
    <%= f.input :memo %> 

    <table> 
     <tr><td>Account</td><td>Debit</td><td>Credit</td><td>Memo</td></tr> 
     <%= f.simple_fields_for :journal_lines do |jl| %> 
      <tr> 
       <td><%= jl.input :finacct_id, :collection => Finacct.all, :label_method => :full_account_name, :value_method => :id, :label => false ,:include_blank => false %></td> 
       <td><%= jl.input :debit, :label => false %></td> 
       <td><%= jl.input :credit, :label => false %></td> 
       <td><%= jl.input :memo, :label => false %></td> 
      </tr> 
     <% end %> 
    </table> 
    <%= submit_tag 'Create' %> 
<% end %> 

我不知道爲什麼我在屏幕上看不到多個日誌行。當我記錄journal_lines對象時,我看到5個空對象,但只有一個顯示在窗體中。

回答

0

好的。我確實得到了答案,但它沒有像回答那樣工作,所以海報刪除了它。然而,很多亂搞的後竟然是該雜誌的模型需要以下

accepts_nested_attributes_for :journal_lines 

有一次,我補充說,所有五個空行出現了表單上。

對方回答還建議我在控制器

@journal = Journal.new 
    5.times do |i| 
     @journal.journal_lines.build 
    end 

不知道這做了一個差異做到這一點,但它在現在。希望這可以幫助。