Reputation: 7762
I am new in Ruby on Rails and i am using Ruby Version 1.9.3 and Rails version 4.0.2.
My query is:
How to get controller and his action name with router prefix in application controller?
See Below my code:-
See Router
root :to=>"home#index"
get "admin/" => "admin/users#index"
get "admin/sign_in" => "admin/users#sign_in"
get "sign_in" => "admin/users#sign_in"
See my application controlle
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :current_user
rescue_from CanCan::AccessDenied do |exception|
flash[:alert] = "Access denied. You are not authorized to access the requested page."
redirect_to root_path and return
end
before_filter :current_user, :get_model
def current_user
# Note: we want to use "find_by_id" because it's OK to return a nil.
# If we were to use User.find, it would throw an exception if the user can't be found.
@current_user ||= User.find_by_id(session[:user_id]) if session[:user_id]
@current_user ||= User.find_by_authentication_token(cookies[:auth_token]) if cookies[:auth_token] && @current_user.nil?
@current_user
end
def authenticate_user
@mydata = params
if current_user.nil?
flash[:error] = 'You must be signed in to view that page.'
redirect_to :admin_sign_in
end
end
end
def authenticate_user
method i have create in application controller.
I want
if current_user.nil? and router prefix is admin Like localhost:3000/admin
it will be redirect on admin sign in path
redirect_to :admin_sign_in
Other then it will be redirect front end sign in part
redirect_to :sign_in
Update my question
My query is how to set condition according to controller and action name with namespace in authenticate_user method where redirect to page.
Please help. How it is possible?
Upvotes: 0
Views: 962
Reputation: 66
Use namespaces in your routes. http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing especially chapter 2.6
2.6 Controller Namespaces and Routing
You may wish to organize groups of controllers under a namespace. Most commonly, you might group a number of administrative controllers under an Admin:: namespace. You would place these controllers under the app/controllers/admin directory, and you can group them together in your router:
namespace :admin do
resources :posts, :comments
end
Also call admin_sign_in_path or sign_in_path instead of symbols. to see all routes in you app use rake routes .
Upvotes: 1