SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Formerly known as RockNRollPig Shpigford's Avatar
    Join Date
    Dec 2002
    Location
    Denver, CO
    Posts
    2,877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Issue updating multiple database rows from single form submission

    So I've got a form that loops through items with a text_field_tag and text_area_tag that lists items in the database to be edited.

    I want to submit this form and have it loop through the items and update the rows based on the ids in the name attribute of the input tag.

    The names are formatted like photos[1234][description].

    Here's what I've got so far but i'm not sure really how to get it functioning correctly in the controller:

    Code Ruby:
    ## View
    <% form_for :photo, :url => { :action => "update_complete" } do |form| %>
    	<% for photo in @photos %> 
    		<%= text_field_tag "photos[#{photo.id}][filename]", photo.filename %>
    		<%= text_area_tag "photos[#{photo.id}][description]", photo.description %>
    	<% end %>
    <% end %>
     
    ## Controller
    def update_complete
    	params[:photos].each do |id|
    		@photo = Photo.find(id)
    		@photo.update_attributes(params[:photos])
    	end
    end

  2. #2
    Formerly known as RockNRollPig Shpigford's Avatar
    Join Date
    Dec 2002
    Location
    Denver, CO
    Posts
    2,877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Man, I've posted this issue just about everywhere and haven't found a soul who can help. This doesn't seem like a hard problem (I could certainly do it with PHP without an issue), but I don't get why no one is able to figure this out.

  3. #3
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,282
    Mentioned
    180 Post(s)
    Tagged
    4 Thread(s)
    In what way is it NOT working for you?
    Have you checked the value of @photo in each iteration through that loop in update_complete?
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this controller code:

    Code ruby:
    ## Controller
    def update_complete
        params[:photos].each do |id,attrs| 
            @photo = Photo.find(id)
            @photo.update_attributes(attrs)
        end
    end


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •