Simple_Fields_For:表单上的多个空行 [英] Simple_Fields_For: Multiple Blank Lines on a Form
问题描述
我正在尝试使用simple_fields_for在一个简单的表单上获得许多空行,但是只显示一行/记录.这是下面的代码,模型,控制器和视图.
I am trying to get a number of blank lines on a simple form using simple_fields_for but only one line/record displays. This is the code below, the models, controller, and view.
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个空对象,但是在表单中仅显示一个.
I'm not sure why I cannot see more than one journal line on the screen. When I log the journal_lines object I see 5 empty objects, but only one displays in the form.
推荐答案
好的.我确实得到了一个答案,但是它没有按照回答的方式工作,因此海报将其删除.但是,经过很多混乱之后,事实证明期刊模型需要以下条件
Okay. I did get an answer but it didn't work as answered so the poster deleted it. However, after a lot of messing around it turned out to be the journal model needed the following
accepts_nested_attributes_for :journal_lines
一旦我添加了,所有五个空白行就会显示在表单上.
Once I added that, all five blank lines showed up on the form.
另一个答案还建议我在控制器中执行此操作
The other answer also suggested that I do this in the controller
@journal = Journal.new
5.times do |i|
@journal.journal_lines.build
end
不确定这是否有所作为,但是现在就可以了.希望这会有所帮助.
Not sure if that made a difference, but it's in now. Hope this helps.
这篇关于Simple_Fields_For:表单上的多个空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!