Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple objects in a Rails form

I want to edit multiple items of my model photo in one form. I am unsure of how to correctly present and POST this with a form, as well as how to gather the items in the update action in the controller.

This is what I want:

<form> <input name="photos[1][title]" value="Photo with id 1" /> <input name="photos[2][title]" value="Photo with id 2" /> <input name="photos[3][title]" value="Custom title" /> </form> 

The parameters are just an example, like I stated above: I am not sure of the best way to POST these values in this form.

In the controller I want to something like this:

@photos = Photo.find( params[photos] ) @photos.each do |photo|     photo.update_attributes!(params[:photos][photo] ) end 
like image 591
Espen Avatar asked Jun 09 '09 22:06

Espen


1 Answers

In Rails 4, just this

<%= form_tag photos_update_path do %>   <% @photos.each do |photo| %>      <%= fields_for "photos[]", photo do |pf| %>       <%= pf.text_field :caption %>       ... other photo fields 
like image 167
mmell Avatar answered Sep 17 '22 18:09

mmell