简体   繁体   中英

How to pass an additional parameter when the user singing up using devise? even it is not User coulmn

I want to send additional values when the user signs up and do some logic before creating the record using devise and devise-JWT in Rails API, I used that https://rubydoc.info/github/heartcombo/devise/main/Devise/ParameterSanitizer to permit additional parameters

choice is not a column in the User table.

application_controller

  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    attributes = [:choice]
    devise_parameter_sanitizer.permit(:sign_up, keys: attributes)
    devise_parameter_sanitizer.permit(:account_update, keys: attributes)
  end


ActionDispatch::Http::Parameters::ParseError (Error occurred while parsing request parameters):

According to Devise documentation, you are able to generate custom Devise controllers, so you may put your own logic.

https://github.com/heartcombo/devise#getting-started

In my case, I added an attribute named "current_client_aplication_id" to the params object for respective request. For knowing how do it, I had to inspect the params objct just in that line, using byebug for debuging.

#application_controller
before_action do
   session[:current_client_application_id] = "1"
   params[request["controller"].singularize]["client_application_id"] = session[:current_client_application_id] if params[request["controller"].singularize]
end

In this way, "current_client application_id" is available in all requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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