0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

rails deivise 複数のモデルストロングパラメーター

Posted at
app/parameters/user/parameter_sanitizer.rb
class Corporation::ParameterSanitizer < Devise::ParameterSanitizer	  
	def initialize(*)
		super
        # 新規登録時のパラメーター
		permit(:sign_up, keys: [
			:email,
			:password, 
			:password_confirmation,
		    :name,
		   	:locate,
		    :start_year
		])   

        # ユーザー情報アップデート時のパラメーター
        permit(:account_update, keys: [
        	:email,
			:password, 
			:password_confirmation,
		    :name,
		   	:locate,
		    :start_year
		]) 
	end
end

こんな感じにストロングパラメーターを指定したいモデルにつき
app下にparametersフォルダを作り、その中にモデル名のフォルダを作り、parameter_sanitizer.rbファイルを作る。

全てのモデルに対して行う
スクリーンショット 2020-12-08 23.57.34.png

こんな感じ

config/application.rb
...
# 上記のものを読み込ませる
config.autoload_paths += Dir[Rails.root.join('app', 'parameters', '**', '*')]
...
application_controller.rb
class ApplicationController < ActionController::Base
	  	def devise_parameter_sanitizer
	  		if resource_class == Student
	  			Student::ParameterSanitizer.new(Student, :student, params) 
	  		elsif resource_class == Corporation
		  		Corporation::ParameterSanitizer.new(Corporation, :corporation, params) 
		  	elsif resource_class == StudentGroup
		  		StudentGroup::ParameterSanitizer.new(StudentGroup, :student_group, params)
		  	elsif resource_class == NonProfitOrganization
		  		NonProfitOrganization::ParameterSanitizer.new(NonProfitOrganization, :non_profit_organization, params)
		  	else
		  		super # Use the default one
		    end
	  	end
end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?