Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple children in single form in rails

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..

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.

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

like image 926
captncraig Avatar asked Apr 10 '10 22:04

captncraig


People also ask

Is there a “rails way” to do multi-step forms?

Unlike regular CRUD interfaces, there’s not a prescribed “Rails Way” to do multi-step forms. This, plus the fact that multi-step forms are often inherently complicated, can make them a challenge.

Why add related models to a Rails application?

Adding related models means establishing meaningful relationships between them, which then affect how information gets relayed through your application’s controllers, and how it is captured and presented back to users through views. In this tutorial, you will build on an existing Rails application that offers users facts about sharks.

How do I create a multi step form?

To summarize the solution to the easy type of multi step form: For each step of your form, create a controller that corresponds to the Active Record model that’s associated with that step. Again, this solution only works if your form steps and your Active Record models have a one-to-one relationship.

What is an example of a form with two steps?

For example, let’s say that you have a form with two steps. In the first step you collect information about a user’s vehicle. The corresponding model to this first step is Vehicle.


1 Answers

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

Episode 196: Nested Model Form, 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>
like image 109
maček Avatar answered Sep 28 '22 02:09

maček