← Back to Upcase

Creating Multiple Objects in One Form

(Nicolo) #1

What is the proper restful way to create/update/edit multiple non-nested objects in a single one submit form?

Is this impossible since creating/updating/editing multiple objects in one form just not restful?

I’ve done this in the past with some routing workarounds like:

<%= form_tag generated_price_path(id: 1), :method => :put do %>
  <%= fields_for "daily_prices[]", daily_price do |f| %>
        <%= f.text_field :price %> 
   <% end %>
<%= submit_tag "Submit" %>

(Geoff Harcourt) #2

@nicolo, if you have a Railscasts Pro subscription, there’s an episode on this. The example uses checkboxes, but a lot of it is applicable to what you’re proposing:


(Nicolo) #3

I have basically implemented what was described in episode 165. This creates a non-restful action to do the update. My question is whether or not there exists a restful or proper way to do this? Or is the example in railscast 165, the proper way?

(Dolph Mullen) #4

You could create a form object, and then delegate the fields to the proper ActiveRecord backed models.

Here is another RailsCast to look at: http://railscasts.com/episodes/416-form-objects.

(Nicolo) #5

Unfortunately, I don’t have RailsCast Pro. I’m kinda getting the impression that I’ll basically should just do this the way it’s done in Railscast 165.