Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 不在生产上工作的路线/在路径上的签名(Rails 5.1)_Ruby On Rails_Devise - Fatal编程技术网

Ruby on rails 不在生产上工作的路线/在路径上的签名(Rails 5.1)

Ruby on rails 不在生产上工作的路线/在路径上的签名(Rails 5.1),ruby-on-rails,devise,Ruby On Rails,Devise,我刚刚更新了我的应用程序,以根据用户角色在登录后重定向用户 例如,如果user.role==“admin”,则应将其带到admin\u root\u路径 如果user.role==“customer”,则应将其带到portal\u root\u路径 出于某种原因,它可以在开发中使用,但当我在生产环境中测试时,它总是在尝试重定向到admin_root_路径时给我一个错误,当时我正在与一个“customer”用户登录。这是生产服务器上的错误 Processing by AdminController

我刚刚更新了我的应用程序,以根据用户角色在登录后重定向用户

例如,如果user.role==“admin”,则应将其带到admin\u root\u路径

如果user.role==“customer”,则应将其带到portal\u root\u路径

出于某种原因,它可以在开发中使用,但当我在生产环境中测试时,它总是在尝试重定向到admin_root_路径时给我一个错误,当时我正在与一个“customer”用户登录。这是生产服务器上的错误

Processing by AdminController#index as HTML Completed 401 Unauthorized in 0ms (ActiveRecord: 0.0ms)
(仅供参考)应用程序的根路径仍然是根路径:“admin#index”,带有before_操作:authenticate_user!在应用程序控制器上

这是我在routes文件上创建根路径的地方:

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
这是我的应用程序_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
class ApplicationController
有什么建议吗?知道为什么它在开发环境和生产环境中表现不同吗

--编辑 这是我的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
class PortalController
结束


非常感谢

你能试着把路线改成这样吗

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

我认为,
root
是默认处理的,因为您在最后说的是
root to:admin#index
,它覆盖了上述所有路由。这只是我的猜测,不过我从来没有这样定义过路线。谢谢你花时间回复。我在生产上再次测试了(它在开发中起作用),而且似乎后签名路径工作正常,因为它指向Admin(如果用户是Admin)和/portal(如果用户是Customer),管理路由工作正常,但是门户索引在生产上断开了。。。(您要查找的页面不存在)。。。我不明白为什么,因为我的控制器和索引页的结构都是一样的。我将在本地运行一个生产环境,看看是否可以获得更好的日志……到目前为止,我认为这与我的PortalController有关,作为一个功能集\u客户,这可能会在生产中起到不同的作用。该函数是:def set_customer@customer=customer.friendly.find(current_user.customer_id)endI刚刚在原始帖子(edit)中添加了PortalController。。。谢谢