口 AAA_checkbox
口 BBB_checkbox
[submit]
-------------------
Model
class Goal < style="font-weight: bold;">View, list.rhtml
<% if task.do_or_not? %>
<%= check_box_tag "task["+task.id.to_s+"][checked]","1",:checked =>"checked"%>
<% else %>
<%= check_box_tag "task["+task.id.to_s+"][checked]","1"%>
<% end %>
<%= hidden_field_tag("task["+task.id.to_s+"][checked]","0") %>
<%= h(task.task_name) %>
-----------------------
從上面的view 我們可以得到這樣的params
params: {
:controller=>"user",
:task=> {
"2"=>{"checked"=>"0"},
... etc...
"4"=>{"checked"=>"1"}
},
:action=>"set_auto_check"
}
---------------------
Controller, user_controler.rb
def set_auto_check
@params[:task].each { |task_id, attr|
task = Task.find(task_id)
task.update_attribute(:checked, attr[:checked])
}
redirect_to :action => 'list'
end
-------------------------
而controller裡會把 "2"放到 task_id, "checked" =>"0" 放到 attr.
這樣便可以完成提交多個check box
沒有留言:
張貼留言