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

前端 未结 4 697
太阳男子
太阳男子 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条回答
  •  春和景丽
    2021-01-06 22:24

    This is the expected behaviour of check_box_tag, as this comment on a rejected fix explains.

    You can use collection_check_boxes like this (haml syntax, sorry):

    # Accumulate todos in a params hash like { todos: { to_complete: [] } }
    = collection_check_boxes(:todos, :to_complete, @incomplete_todos, :id, :name) do |todo_builder|
      = todo_builder.label do
        # This is the result of calling :name on the todo, as specified
        # calling the helper
        = todo_builder.text
        = todo_builder.check_box
    

    Of course you can use partials inside the block, just pass and use the builder inside.

    Check more options in the API docs.

提交回复
热议问题