How can I save an entire list of items true or false?
- by JZ
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