Re: [問題] 關於check_box的問題

看板Ruby作者 (lake)時間16年前 (2007/12/01 22:16), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/3 (看更多)
※ 引述《ilake (lake)》之銘言: : 環境:Ruby 1.8.6, RoR 1.2.5 解決了, 自己回一下 -------------------- view, list.rhtml <% form_tag :action => 'set_check', :goal_id => goal do %> <% for task in goal.tasks %> <%= check_box_tag "task["+task.id.to_s+"][checked]","1"%> <%= hidden_field_tag("task["+task.id.to_s+"][checked]","0") %> <%= h(task.name) %> <% end %> <%= submit_tag "submit" %> <% end %> 從上面的view 我們可以得到這樣的params params: {  :controller=>"user",  :task=> {   "2"=>{"checked"=>"0"},   ... etc...   "4"=>{"checked"=>"1"}  },  :action=>"set_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 而這邊會把"2"放到 task_id, "checked" => "0" 放到 attr. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.117.162.137
文章代碼(AID): #17KMoz7J (Ruby)
文章代碼(AID): #17KMoz7J (Ruby)