How do I set a unique ID for checkboxes in a multi-record Rails form?

前端 未结 4 700
太阳男子
太阳男子 2021-01-06 21:55

I\'ve set up a Rails form roughly following the instructions in this Railscast.

Here\'s the code for the form:

<% form_tag complete_todos_path, :m         


        
4条回答
  •  Happy的楠姐
    2021-01-06 22:39

    I ended up using a solution similar to Ryan's, but as I wrote in the comment I had to make a further change. In the form:

    <%= check_box_tag "todo_ids[#{todo.id}]", todo.id %>
    

    In the action called by the form:

    Todo.update_all(["completed_at = ?", Time.now], :id => params[:todo_ids].keys)
    

    Note the "params[:todo_ids].keys" at the end, which was a workaround to deal with the odd way the parameters were formatted:

    "todo_ids" => {"5"=>"5"}
    

提交回复
热议问题