Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails routes logic

Is it OK to put custom logic in routes.rb?

For example:

unless current_user
  root :to => anonymous_page
else
  root :to => logged_in_page
end
like image 701
cjm2671 Avatar asked Oct 09 '22 16:10

cjm2671


1 Answers

You can put custom logic into routes... but as avenger suggested - "current_user" won't work due to when the routes file is loaded. We sometimes use logic in our routefile (eg setting up routes that are only available if RAILS_ENV == 'development').

What you probably want is a before_filter on "anonymous_page" eg:

before_filter :redirect_if_logged_in, :only => :anonymous_page

def redirect_if_logged_in
  redirect_to logged_in_page if current_user.present?
end
like image 128
Taryn East Avatar answered Oct 13 '22 12:10

Taryn East