【问题标题】:Simple_Fields_For: Multiple Blank Lines on a FormSimple_Fields_For:表单上有多个空行
【发布时间】:2014-09-23 03:28:32
【问题描述】:

我正在尝试使用 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 个空对象,但表单中只显示一个。

【问题讨论】:

    标签: ruby-on-rails simple-form fields-for


    【解决方案1】:

    好的。我确实得到了答案,但它没有像回答的那样起作用,所以海报删除了它。然而,经过一番折腾,结果发现是期刊模型需要以下内容

    accepts_nested_attributes_for :journal_lines
    

    我添加后,所有五个空白行都显示在表单上。

    另一个答案也建议我在控制器中执行此操作

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

    不确定这是否有所作为,但现在已经有了。希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-11
      相关资源
      最近更新 更多