Saving check_box_tag state after form submission error

I use check_box_tag in the form helper, and all other fields retain their inputs after a submit verification error, but check_box_tag does not. Is there a way to get check_box_tag to save its state in a failed submit form? Here is the code:

<%= check_box_tag 'user_ids[]', user.id, false, :class => 'user_checkbox' %> 

I need to use check_box_tag instead of check_box in this context.

+4
source share
1 answer

You pass false to check_box_tag , so all checkboxes are disabled. To fix this, you can do something like:

 <%= check_box_tag 'user_ids[]', user.id, params[:user_ids].include?(user.id), :class => 'user_checkbox' %> 

It checks if user.id among the user IDs that were sent, and if it was then, the checkbox is selected.

+8
source

Source: https://habr.com/ru/post/1384535/


All Articles