Use custom id for check_box_tag in Rails

前端 未结 7 1005
时光说笑
时光说笑 2021-02-04 03:36

How do you set a custom id when using a check_box_tag helper in rails?

I have a loop which creates a bunch of checkboxes based on a collection:

- subject         


        
7条回答
  •  梦如初夏
    2021-02-04 03:59

    @Ganesh, in your solution the resulting params hash has the following form:

    params[:question][:syllabus_reference] = {1 => 1, 2 => 2, 3 => 3, ...}
    

    These should work better for you:

    check_box_tag "question[syllabus_reference][]", sr.id, checked, {:id => "question_syllabus_reference_#{sr.id}"
    

    Notice the third parameter (checked) is required for this to work. The resulting params array will be

    params[:question][:syllabus_reference] = {1, 2, 3, ...}
    

提交回复
热议问题