【发布时间】:2015-12-21 16:12:15
【问题描述】:
我正在尝试为登录时触发的用户构建一个“路由器”。我正在尝试按角色重定向用户。
我有以下:
require "#{Rails.root}/lib/client/user_router"
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
add_flash_types :info
def after_sign_in_path_for(user)
UserRouter.new(user)
end
结束
在 /lib/user_router.rb
中 class UserRouter
include Rails.application.routes.url_helpers
def initialize(user)
@user = user
route_user_by_role
end
def route_user_by_role
if @user.is_pt_master?
pt_master_root_url
elsif @user.is_traveler?
traveler_root_url
elsif @user.client_user_role?
route_client_by_role
else
root_url
end
end
def route_client_by_role
if @user.is_super_admin?
for_super_admin
else
for_other_admin
end
end
def for_super_admin
if @user.client_account.blank?
edit_client_account_url
else
client_root_url
end
end
def for_other_admin
if @user.is_first_sign_in? || @user.client_user_info.blank?
edit_client_user_info_url(@user)
else
client_root_url
end
end
end
我正在使用 _url 因为如果我使用 _path 我会收到一个 .to_model 错误,但是使用 _url 我会得到 缺少要链接的主机!请提供 :host 参数,设置 default_url_options[:host],或设置 :only_path 为 true
我已经在 config/environments 中将默认主机设置为 localhost:3000。任何有关如何执行此操作的帮助将不胜感激。
【问题讨论】:
标签: ruby-on-rails ruby