我刚刚根据用户角色登录后更新了应用程序以重定向用户。
例如,如果user.role ==“ admin”,则应将其带到admin_root_path ...
,如果user.role ==“ customer”,则应将其带到portal_root_path。
由于某种原因,它可以在DEVELOPMENT上运行,但是当我在PRODUCTION上进行测试时,尝试始终重定向到admin_root_path时出现错误,我使用的是“客户”用户登录。这是PRODUCTION服务器上的错误。
Processing by AdminController#index as HTML Completed 401 Unauthorized in 0ms (ActiveRecord: 0.0ms)
(FYI)应用程序的root_path仍然是root:“ admin#index”,具有before_action:authenticate_user!在应用程序控制器上...
这是我在路线文件上创建根路径的地方:
Rails.application.routes.draw do
get "admin/index"
scope :portal, as: 'portal' do
root to: "portal#index"
end
scope :admin, as: 'admin' do
root to: "admin#index"
end
root to: "admin#index"
end
这是我的application_controller.rb
class ApplicationController < ActionController::Base
include Pundit
protect_from_forgery with: :exception, prepend: true
before_action :authenticate_user!
def after_sign_in_path_for(resource)
if resource.role == "admin"
admin_root_path
elsif resource.role == "customer"
portal_root_path
else
super
end
end
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
private
def user_not_authorized
flash[:alert] = "You are not authorized to perform this action."
redirect_to(request.referrer || portal_root_path)
end
end
有什么建议吗?知道为什么它在DEV和PRODUCTION环境中表现不同吗?
-编辑 这是我的PortalController,该视图使用变量@customer来显示客户的信息。
class PortalController < ApplicationController
before_action :set_customer
def index
end
private
# Use callbacks to share common setup or constraints between actions.
def set_customer
@customer = Customer.friendly.find(current_user.customer_id)
end
结束
非常感谢!
答案 0 :(得分:0)
您可以尝试更改路线以使其看起来像这样吗?
scope :portal do
resources :portals, only: :index
// if you don't want to use resources, just define a static route
end
scope :admin do
resources :admins, only: :index
// if you don't want to use resources, just define a static route
end