How can I save an entire list of items true or false?

Posted by JZ on Stack Overflow See other posts from Stack Overflow or by JZ
Published on 2010-04-15T23:34:15Z Indexed on 2010/04/16 2:43 UTC
Read the original article Hit count: 375

Filed under:
|

I'm following Ryan Bates, Railscast episode 52 and I've translated relevant parts of the code to work with Rails 3.0.0.beta2. In Ryan's case, he simply marks items incomplete and saves a timestamp. If an Item contains a timestamp the model returns the item in the completed list.

I'm attempting to save ALL values true or false, depending on whether the check_box_tag is selected or not (using boolean). I am able to save ONLY selected items, true or false. How can I save an entire list of items true or false, depending on whether the checkbox is selected? The following is my attempt:

controller logic:

 def yardsign
    Add.update_all(["yardsign=?", true], :id => params[:yard_ids])
    redirect_to adds_path
  end

html.erb:

<%= form_tag yardsign_adds_path, :method => :put do %>
<% @adds.each do |add| %>
  <td><%= check_box_tag "yard_ids[]", add.id %></td>
    <% end %>
<% end %>

routes.rb

resources :adds do
    collection do
     put :yardsign 
  end
end

Terminal

Started POST "/adds/yardsign" for 127.0.0.1 at 2010-04-15 19:22:49
  Processing by AddsController#yardsign as HTML
  Parameters: {"commit"=>"Update", "yardsigntakers"=>["1", "2"], "authenticity_token"=>"3arhsxg/Ky+0W7RNM2T3QditMTJmOnLR5CqmMYWN4Qw="}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
  SQL (1.8ms)  UPDATE "adds" SET yardsign='t' WHERE ("adds"."id" IN (1, 2))
Redirected to http://localhost:3000/adds

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about checkbox