【问题标题】:Trying to call _path or _url from class in /lib (ROR)尝试从 /lib (ROR) 中的类调用 _path 或 _url
【发布时间】: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


    【解决方案1】:

    default_url_options 添加到UserRouter

    class UserRouter
      include Rails.application.routes.url_helpers
    
      def self.default_url_options
        {host: 'localhost:3000'}
      end
    
      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 <= breaks here with undefined method error
        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
    

    【讨论】:

    • 这工作到一定程度,它在调用“route_client_by_role”的那一行中断了: undefined method `to_model' for #<0x007faaaa261d10>
    【解决方案2】:

    所以我不能像我想要的那样将它作为 /lib/ 中的类来做,但是将它作为控制器问题拉到一个模块中允许我将逻辑与 ApplicationController分开> 就像我想的那样,它并不理想,但总比把它全部塞进 App Cntrl 好。

    class ApplicationController < ActionController::Base
      include UserRouter
      ...    
    
      def after_sign_in_path_for(user)
        initialize_user_router(user)
      end
    
      ...
    end
    
    
    module UserRouter
      def initialize_user_router(user)
        @user = user
        direct_user_by_role
      end
    
      def direct_user_by_role
        if @user.is_pt_master?
          pt_master_root_path
        elsif @user.is_traveler?
          direct_traveler
        elsif @user.client_user_role?
          direct_client_by_role
        else
          root_path
        end
      end
      def direct_traveler
        ...
        traveler_root_path
      end
    
      def direct_client_by_role
        if @user.is_super_admin?
          direct_super_admins
        else
          direct_other_admins
        end
      end
    
      def direct_super_admins
        if @user.client_account.blank?
          edit_client_account_path
        else
          client_root_path
        end
      end
    
      def direct_other_admins
        if @user.first_sign_in? || @user.client_user_info.blank?
          edit_client_user_info_path(@user)
        else
          client_root_path
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2012-08-10
      • 1970-01-01
      • 2011-05-12
      • 2014-09-05
      • 2016-06-24
      • 2011-01-21
      • 2011-12-21
      • 2014-04-20
      • 1970-01-01
      相关资源
      最近更新 更多