Rails 3 - how to save (un)checked checkboxes?

前端 未结 1 1836
悲哀的现实
悲哀的现实 2021-02-03 10:39

I have in a form (form_tag) several checkboxes like this:

<%=check_box_tag \'model_name[column_name]\', 1, (@data.model_name.column_name == 1         


        
1条回答
  •  北海茫月
    2021-02-03 10:53

    This happens because an unchecked checkbox will not send any value to the server. To circumvent this Rails provides the check_box helper, which generates code like this:

    
    
    

    Alternatively, insert a hidden field with hidden_field_tag:

    <%= hidden_field_tag 'model_name[column_name]', '0' %>
    <%= check_box_tag 'model_name[column_name]', 1, (@data.model_name.column_name == 1 ? true : false) %>
    

    0 讨论(0)
提交回复
热议问题