In Rails, how to handle multiple checked checkboxes, just split on the , or?

前端 未结 5 999
遇见更好的自我
遇见更好的自我 2020-12-02 20:43

Curious what the \'rails way\' of handling the situation when a user checks multiple checkboxes (with the same name value), and it gets posted back to the controller.

<
5条回答
  •  醉酒成梦
    2020-12-02 21:08

    The easiest way of doing this is to set those checkboxes up to become an array.

    HTML:

    
    
    
    

    Controller:

    tag_ids = params[:tag_ids]
    

    (Of course, you'd probably be using form_for-based helpers in the view, and therefore mass-assigning the tag IDs. This is just the most generic example.)

提交回复
热议问题