铁轨中单个形式的多个孩子

时间:2022-12-01 10:51:12

I have a model that has an arbitrary number of children entities. For simplicity lets call the entities Orders and Items. I would like to have a create Orders form where I input the order information, as well as add as many items as I want. If I click the "Add another item" button, a new set of form elements will be added to input the new data, amounts, etc..

我有一个具有任意数量的子实体的模型。为简单起见,我们可以调用实体Orders和Items。我想有一个创建订单表单,我输入订单信息,以及添加任意数量的项目。如果单击“添加其他项”按钮,将添加一组新的表单元素以输入新数据,金额等。

I could hack this out in pure javascript, but I'm pretty sure there has to be a more magical, railsish way to do it, maybe with a partial view or something. I'm just a little too new to rails to know what it is.

我可以用纯粹的javascript来解决这个问题,但是我很确定必须有一个更神奇,更有轨可行的方法来实现它,可能是部分视图或其他东西。我只是对铁路有点太新了解它是什么。

What is the best way to dynamically add the new form elements, and then to access them in the create controller?

动态添加新表单元素然后在创建控制器中访问它们的最佳方法是什么?

1 个解决方案

#1


15  

Can't beat this Railscasts.com tutorial provided by Ryan Bates.

无法击败Ryan Bates提供的Railscasts.com教程。

Episode 196: Nested Model Form, pt. 1

第196集:嵌套模型表,pt。 1

Here's an example that works with just a single level of nesting

这是一个只使用一个嵌套级别的示例

Models

models/company.rb

class Company < ActiveRecord::Base
  has_many :people, :dependent => :destroy
  accepts_nested_attributes_for :people, :allow_destroy => true
end

models/person.rb

class person < ActiveRecord::Base
  belongs_to :company
end

Controllers

companies_controller.rb

def new
  @company = Company.new
  3.times { person = @company.people.build }
end

Views

views/companies/_form.html.erb

<% form_for @company do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>

  <%= f.fields_for :people do |builder| %> 
    <%= render "people_fields", :f => builder %>
  <% end %>

  <p><%= f.submit "Submit" %></p>
<% end %>

views/companies/_people_fields.html.erb

<p>
  <%= f.label :name, "Person" %>
  <%= f.text_field :name %>
  <%= f.check_box :_destroy %>
  <%= f.label :_destroy, "Remove" %>
</p>

#1


15  

Can't beat this Railscasts.com tutorial provided by Ryan Bates.

无法击败Ryan Bates提供的Railscasts.com教程。

Episode 196: Nested Model Form, pt. 1

第196集:嵌套模型表,pt。 1

Here's an example that works with just a single level of nesting

这是一个只使用一个嵌套级别的示例

Models

models/company.rb

class Company < ActiveRecord::Base
  has_many :people, :dependent => :destroy
  accepts_nested_attributes_for :people, :allow_destroy => true
end

models/person.rb

class person < ActiveRecord::Base
  belongs_to :company
end

Controllers

companies_controller.rb

def new
  @company = Company.new
  3.times { person = @company.people.build }
end

Views

views/companies/_form.html.erb

<% form_for @company do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>

  <%= f.fields_for :people do |builder| %> 
    <%= render "people_fields", :f => builder %>
  <% end %>

  <p><%= f.submit "Submit" %></p>
<% end %>

views/companies/_people_fields.html.erb

<p>
  <%= f.label :name, "Person" %>
  <%= f.text_field :name %>
  <%= f.check_box :_destroy %>
  <%= f.label :_destroy, "Remove" %>
</p>