I need help implementing Parameter sanitisers for multiple devise models

307 views
Skip to first unread message

Jovan Mulepi

unread,
Jan 12, 2017, 10:52:16 AM1/12/17
to Devise
Hello, 

I am kinda a newbie in the Rails Arena and was asking for help on how to implement parameter sanitisers for multiple devise models
According to devise wiki, 

If you have multiple Devise models, you may want to set up a different parameter sanitizer per model. In this case, we recommend inheriting from Devise::ParameterSanitizer and adding your own logic:

class User::ParameterSanitizer < Devise::ParameterSanitizer
  def initialize(*)
    super
    permit(:sign_up, keys: [:username, :email])
  end
end

And then configure your controllers to use it:

class ApplicationController < ActionController::Base
  protected

  def devise_parameter_sanitizer
    if resource_class == User
      User::ParameterSanitizer.new(User, :user, params)
    else
      super # Use the default one
    end
  end
end

My question is where in the directory structure do i save the new ParameterSanitizer class file.
 

Jason Fleetwood-Boldt

unread,
Jan 12, 2017, 11:34:41 AM1/12/17
to plataforma...@googlegroups.com

> On Jan 12, 2017, at 10:52 AM, Jovan Mulepi <mulepi...@gmail.com> wrote:
>
> My question is where in the directory structure do i save the new ParameterSanitizer class file.
>

Don't be brainwashed by Rails' folder structure (app/models, app/views, app/controller). You just make a new folder called "app/sanitizers" ...or anything really, inside the app/ folder.

However, you must then explicitly load what's in that folder. this is typically done in application.rb in a config.to_prepare statement

Here's a sample of how to load it from your initializer, assuming you had a folder called app/sanitizers/ and in that folder you had your object:



config.to_prepare do
# Load stuff not inside models, views, controllers
[ "../app/sanitizers/*.rb" ].each do |x|
Dir.glob(File.join(File.dirname(__FILE__), x)) do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end
end




----

Jason Fleetwood-Boldt
te...@datatravels.com
http://www.jasonfleetwoodboldt.com/writing

If you'd like to reply by encrypted email you can find my public key on jasonfleetwoodboldt.com (more about setting GPG: https://gpgtools.org)


Reply all
Reply to author
Forward
0 new messages