Category

Devise

Adde controllers concerns for devise params

By Devise, Ruby on Rails No Comments

Imagine that you decide to add a new user registration field. Let it be the name field After adding a field to the View You must add information about the new field to the controller. You can add it to the application_controller.rb file, but this is not the best solution. Imagine that your application will become very large. Imagine what your controller will look like. To get rid of this problem, I use modules

1 Step. Create module in foulder controllers/concerns. Create file, let’s say devise_whitelist.rb

module DeviseWhitelist
  extend ActiveSupport::Concern
    included do before_flter :configura_permitted_parameters, if: :devise_controller?
  end
  def configura_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
    devise_parameter_sanitizer.permit(:account_update, keys: [:name])
  end
end

2 Step. Include module to main controller application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  
  include DeviseWhitelist
end