Use custom id for check_box_tag in Rails

前端 未结 7 1004
时光说笑
时光说笑 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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-04 04:01

    I used this in my application to create checkbox tags from collection and submit an array of them:

    <% @cursos.each do |c| %>
      
        <%= check_box_tag "vaga[curso_ids][]",
          c.id, (checked = true if form.object.curso_ids.include?(c.id)) %>
        <%= label_tag "vaga[curso_ids][][#{c.id}]", c.nome %>
      
    <% end %>
    

    So in params, I have an array "curso_ids"=>["1", "3", "5"] instead of a string "curso_ids"=>"5". If you want to return a single value, use vaga[curso_id], otherwise use vaga[curso_ids][] to return an array.

提交回复
热议问题