user4713761
user4713761

Reputation:

Trying to call _path or _url from class in /lib (ROR)

I am trying to build a "router" for users that fires on login. I am trying to redirect users by role.

I have the following:

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

end

and in /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

I am using _url because if I use _path I get a .to_model error, but with _url I am getting Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

I have already set default host in config/environments to be localhost:3000. Any help with how to do this would be greatly appreciated.

Upvotes: 0

Views: 123

Answers (2)

user4713761
user4713761

Reputation:

So I wasn't able to do it as Class in /lib/ like I wanted to, but pulling it out into a module as a Controller concern allowed me to keep the logic separate from the ApplicationController like I wanted to, its not ideal, but better than stuffing it all into 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

Upvotes: 0

Inpego
Inpego

Reputation: 2667

Add default_url_options to 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

Upvotes: 0

Related Questions