Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preserving state of check_box_tag after form submit validation error

I am using check_box_tag in a form helper and all the other fields preserve their inputs after a submit validation error, but check_box_tag does not. Is there some way to get check_box_tag to save its state on a failed form submit? Here's 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.

like image 618
tks Avatar asked Feb 22 '23 05:02

tks


1 Answers

You're passing false to check_box_tag so all checkboxes are disabled. To fix that you can do smth like:

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

It checks whether user.id was among user ids that were submitted and if it was then checkbox is checked.

like image 148
KL-7 Avatar answered Apr 19 '23 22:04

KL-7