![](/img/trans.png)
[英]Rails 4 + Devise 3 - How to permit parameters for update action?
[英]How to permit a new parameter in rails (devise) when implementing role based authorization
我正在創建具有基於角色的授權的應用程序。因此,在我中,我創建了一個遷移以設計用戶以添加新列“ role”,並且我的應用程序控制器中具有以下代碼塊以允許新參數(role) )。但是當我嘗試注冊為新用戶時,仍然收到錯誤消息,指出參數角色不被允許。請幫助我解決此問題。
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit( :email, :password, :password_confirmation, roles: [] ) }
end
end
這就是我在用戶模型中得到的
class User < ApplicationRecord
belongs_to :role
# has_many :Product
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
ROLES = %i[admin manager customer]
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation, :role)
end
end
遷移如下
class AddRoleToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :role, :string
end
end
請幫我解決這個問題。謝謝。
您的用戶模型無權訪問params,因此您可以從那里刪除user_params方法。 除非您要嵌套屬性,否則無需為角色屬性傳遞數組,因此請進行更改
devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit( :email, :password, :password_confirmation, roles: [] ) }
至
devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit( :email, :password, :password_confirmation, :role ) }
#
而且您應該很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.