Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different page for logged in user and not logged in user at root

I want to show different root page for users in Rails.

I defined the root:

root :to => 'welcome#index'

And the welcome control:

class WelcomeController < ApplicationController
  before_filter :authenticate_user!

  def index
  end

end

Currently it is ok for logged in users, but the not logged in users redirected to /users/sign_in

I want to show static root page and not redirect.

like image 258
Bruce Dou Avatar asked Feb 18 '12 07:02

Bruce Dou


3 Answers

The answer, suggested by Puneet Goyal will not work in Rails 4. See this. The solution is to use an alias for one of the two routes like this:

authenticated do
  root :to => 'welcome#index', as: :authenticated
end

root :to => 'home#static_page'
like image 172
Alexander Popov Avatar answered Nov 15 '22 20:11

Alexander Popov


This answer should work. This was posted on the page Bradley linked.

Put this in your Welcome controller.

def index
  if authenticate_user?
    redirect_to :controller=>'dashboard', :action => 'index'
  else
    redirect_to '/public/example_html_file.html'
  end
end
like image 21
icantbecool Avatar answered Nov 15 '22 20:11

icantbecool


In your routes.rb :

authenticated do
  root :to => 'welcome#index'
end

root :to => 'home#static_page'

This will ensure that root_url for all authenticated users is welcome#index

For your reference: https://github.com/plataformatec/devise/pull/1147

like image 2
pungoyal Avatar answered Nov 15 '22 21:11

pungoyal