Я только что обновил свое приложение, чтобы перенаправить пользователя после входа в систему на основе его роли пользователя.
Например, если user.role == "admin", он должен перейти к admin_root_path...
и если user.role == "customer", он должен перейти к portal_root_path.
По какой-то причине это работает на РАЗРАБОТКЕ, но когда я тестировал на ПРОДУКЦИИ, это дало мне ошибку, пытаясь все время перенаправить на admin_root_path, я входил в систему с пользователем «клиент». Это ошибка на PRODUCTION сервере.
Processing by AdminController#index as HTML Completed 401 Unauthorized in 0ms (ActiveRecord: 0.0ms)
(К вашему сведению) 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
конец
Большое спасибо!