Search code examples
ruby-on-railsacts-as-taggable-on

devise form_for, how to use array of check_box_tag


I use acts-as-taggable-on to save multiple user interests. I try to show a fixed list of checkboxes on the signup form. The code below works.

= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|
%p
  - ["music","art","movies"].each do |interest|
    = check_box_tag "user[interested_list][]", interest

Problem is that when hitting "Submit" and if page reload because some field was missing I do not see the checkboxes previously checked, They're all reset.

I understand I'm missing the value, but I been trying using something like this but it doesn't work

= check_box_tag "user[interested_list][]", interest, params[:user][:interested_list][interest]

I get the following error

undefined method `[]' for nil:NilClass

Any idea?


Solution

  • Solved.

    I just needed to check if the value was present inside the array.

    = check_box_tag "user[interested_list][]", interest, session[:interested_list].include?(interest)