简体   繁体   English

使用 Devise 和 simple_form 进行多选

[英]Multiselect with Devise and simple_form

I have a rails application that uses Devise and simple_form to register and authenticate a user.我有一个使用 Devise 和 simple_form 来注册和验证用户的 rails 应用程序。 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我跟着这个: 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注册控制器.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: _registration.html.haml:

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

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: _registration.html.haml:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM