我正在关注Ryan Bates,Railscast episode 52并且我已经翻译了代码的相关部分以使用Rails 3.0.0.beta2。在Ryan的案例中,他只是标记项目不完整并保存时间戳。如果项目包含时间戳,则模型将返回已完成列表中的项目。
我正在尝试保存所有值为true或false ,具体取决于是否选中了check_box_tag(使用boolean)。我只能保存所选项目,无论是真还是假。如何将整个项目列表保存为true或false,具体取决于是否选中了复选框?以下是我的尝试:
控制器逻辑:
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
终端
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