Forum: Ruby on Rails Updating multiple records from a hash

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
241b11f59c4bcfc2ce84a43f52b30001?d=identicon&s=25 Sean Cahoon (osakaboy)
on 2007-06-07 03:49
I'm trying to figure out how to update multiple records in one method
from a hash sent by a view, and unfortunately, i'm dead stuck.

Here's what i'm sending:

<% form_tag :controller => 'todoitems', :action => 'toggle_todo', :id =>
@id do -%>
<% if @todoitems then @todoitems.each do |t| -%>
    <div class="rev_todo_box">
      <%= check_box("to_toggle",, {:checked => t.done}, TRUE,
      <%= t.description %>
    <% end -%>
  <% end -%>
  <%= submit_tag 'Update' %>
<% end %>

This is what gets sent:

Parameters: {"commit"=>"Update", "to_toggle"=>{"6"=>"true",
"7"=>"false", "8"=>"false", "9"=>"true"}, "action"=>"toggle_todo",
"id"=>"8", "controller"=>"todoitems"}

And this is the controller def that *should* modify the records for me,
but it doesn't do anything. I commented it out so people can see what
I'm trying to do:

def toggle_todo
    #takes the hash out of params and assigns it to @toggle
    @toggle = params[:to_toggle]
    if @toggle
      #iterate over the hash
      @toggle.each do |id, value|
        #find the record
        @todoitem = Todoitem.find_by_id(id)
        #change the record's "done" column to the right value
        @todoitem.done = value
    redirect_to :controller => 'showproject', :action => 'show', :id =>

Each todoitem has a "done" column that's a boolean to show if it's done
or  not.

Anybody see what I'm missing? Thanks!
241b11f59c4bcfc2ce84a43f52b30001?d=identicon&s=25 Sean Cahoon (osakaboy)
on 2007-06-07 04:27
Ignore this thread. The server burped when I was submitting it, and I
ended up sending it twice. Sorry!
This topic is locked and can not be replied to.