Home > Net >  Multiselect with Devise and simple_form
Multiselect with Devise and simple_form

Time:06-04

I have a rails application that uses Devise and simple_form to register and authenticate a user. I wanted to add another table of preferences such that a user can have multiple preferences and a preference can be assigned to multiple users.

I followed this: https://dev.to/neshaz/join-table-in-rails-23b5

and made the respective associations with a join table and I am able to view the checkboxes multiselect on my form. But I am stuck on the part on processing the params in my registration controller page. For context this is what i have:

registration_controller.rb

    def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up) do |u|
  u.permit({ account_attributes: [:username], invite_request_attributes: [:text] }, :company, :email, :password, :password_confirmation, :invite_code, :agreement, :website, :confirm_password)
end

end

_registration.html.haml:

- for preference in Preference.all
  = check_box_tag "preference[user_ids][]", preference.id
  = h preference.pref

CodePudding user response:

You try this way.

class ApplicationController < ActionController::Base

  before_action :configure_permitted_parameters, if: :devise_controller?

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name, preference_ids: []])
    devise_parameter_sanitizer.permit(:account_update, keys: [:name, :website, :bio])
  end

end

_registration.html.haml:

- for preference in Preference.all
  = check_box_tag "user[preference_ids][]", preference.id
  = h preference.pref
  • Related