嵌套表单,使用form_with和fields_for,迭代不起作用

时间:2017-11-04 14:52:22

标签: ruby-on-rails

这让我发疯了。可能我忽略了一些显而易见的事情,但我已经阅读了同一主题的所有其他帖子,无法解决问题。提前感谢您的帮助。

我有一个模型Klasse和一个模型Klpklasse has many klps的一个实例。我想以与klasse相同的形式创建klp。我使用form_withfields_for进行了设置,而klasses_controller.rb我有3.times { @klasse.klps.build }但这次迭代似乎不起作用。 fields_for块仅显示一次而不是3次。

new.html中的表单是根据rails“入门”指南和“表单助手”指南后面的fields_for块创建的。

这是我的代码:

应用\模型\ klasse.rb:

class Klasse < ApplicationRecord
    has_many :klps
    has_many :people, :through => :klps
    accepts_nested_attributes_for :klps, allow_destroy: true, :reject_if => :all_blank

    validates :name, presence: true,
                  length: { minimum: 1 },
                  uniqueness: true

    validates :klp_std_soll, presence: true,
                  length: { minimum: 1 }

   def klp_std_ist
       self.klps.sum("std")
   end

end

应用\模型\ person.rb

class Person < ApplicationRecord
  has_many :klps
  has_many :klasses, :through => :klps

  validates :name, presence: true,
                  length: { minimum: 1 },
                  uniqueness: true
  validates :vorname, presence: true,
                  length: { minimum: 1 }

  def last_and_first_name
    "#{name}, #{vorname}"
  end

end

应用\模型\ klp.rb

class Klp < ApplicationRecord
    belongs_to :klasse
    belongs_to :person
end

应用\控制器\ klasses_controller.rb

class KlassesController < ApplicationController

    def new
        @klasse = Klasse.new
        3.times { @klasse.klps.build }
    end

    private
    def klasse_params
        params.require(:klasse).permit(:name, :klassentyp_id, :klp_std_soll, klps_attributes: [:id, :person_id, :std, :_destroy])
    end

end

应用\控制器\ klps_controller.rb

class KlpsController < ApplicationController

    def new
        @klp = Klp.new
    end

    private
    def klp_params
        params.require(:klp).permit(:klasse_id, :person_id, :std)
    end
end

应用\视图\ klasses \ new.html.erb

<%= form_with scope: :klasse, url: klasses_path, local: true do |form| %>

     <ul>
         <%= form.fields_for :klps do |klps_form| %>
            <p>
                <%= klps_form.label :person %>
                <%= klps_form.collection_select :person_id, Person.all, :id, :last_and_first_name %>
                <%= klps_form.label :std %>
                <%= klps_form.text_field :std %>
            </p>

        <% end %>
    </ul>

    <p>
        <%= form.submit %>
    </p>
<% end %>

3 个答案:

答案 0 :(得分:1)

尝试将klps的集合设置为一个实例变量,然后您可以将其作为显式“记录对象”参数传递给fields_for方法:

应用/控制器/ klasses_controller.rb

def new
  @klasse = Klasse.new
  @klps = 3.times { @klasse.klps.build }
end

应用\视图\ klasses \ new.html.erb

<%= form.fields_for :klps, @klps do |klps_form| %>
  ... your form...
<% end %>

另请参阅:How do I pass an array to fields_for in Rails?

参考:https://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for

答案 1 :(得分:0)

我举一个例子见

customer.rb

onlogin

address.rb

class Customer < ApplicationRecord
   has_many :addresses, dependent: :destroy
   accepts_nested_attributes_for :addresses, allow_destroy: true
end

customers_controller.rb

class Address < ApplicationRecord
   belongs_to :customer
end

new.html.haml

class CustomersController < ApplicationController
  def new
    @customer = Customer.new
    3.times {
      @customer.addresses.build //You can use @customer.addresses.new
    }
  end
end

  def customer_params
    params.require(:customer).permit(:id, :name, addresses_attributes:[:id,:country, :state])
  end

查看/地址/ _fields.html.haml

= simple_form_for @customer do |f|
  = f.input :name, label: false, wrapper: false, input_html: { class: "gui-input"}, placeholder: "Customer Name"
  = f.fields_for :addresses do |a|
    = render 'addresses/fields', a: a

了解更多信息

https://www.pluralsight.com/guides/ruby-ruby-on-rails/ruby-on-rails-nested-attributes

答案 2 :(得分:0)

使用

<%= form.fields_for :klps_attributes do |klps_form| %>