Rails 3 w/ Devise: How to set two separate homepages based on whether the user is authenticated or not?

孤街浪徒 提交于 2019-12-02 19:42:45

Try this, it's specific to Warden/Devise though.

root to: "dashboard#index", constraints: lambda { |r| r.env["warden"].authenticate? }
root to: "homepage#index"

In your HomeController:

def index
  if !user_signed_in?
    redirect_to :controller=>'dashboard', :action => 'index'
  end
end
pungoyal

(Exact same question answered here: https://stackoverflow.com/a/16233831/930038. Adding the answer here too for others' reference.)

In your routes.rb :

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

root :to => 'homepage#index'

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

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

Here's the correct answer with rails 4

root to: 'dashboard#index', constraints: -> (r) { r.env["warden"].authenticate? },
         as: :authenticated_root
root to: 'homepage#index'

I tried to add this to / edit the accepted answer but it's too much of an edit to be accepted apparently. Anyway, vote for the accepted answer (from Bradley), it helped me come up with this one :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!