登录用户的不同页面,未登录root用户

我想在Rails中为用户显示不同的根页面。

我定义了根:

root :to => 'welcome#index' 

欢迎控制:

 class WelcomeController < ApplicationController before_filter :authenticate_user! def index end end 

目前可以登录用户,但未登录的用户重定向到/ users / sign_in

我想显示静态根页面而不是重定向。

Puneet Goyal建议的答案在Rails 4中不起作用。看到这个 。 解决方案是使用两个路由之一的别名,如下所示:

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

这个答案应该有效。 这张贴在Bradley链接的页面上。

把它放在欢迎控制器中。

 def index if authenticate_user? redirect_to :controller=>'dashboard', :action => 'index' else redirect_to '/public/example_html_file.html' end end 

在您的routes.rb

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

这将确保所有经过身份validation的用户的root_urlwelcome#index root_url

供您参考: https : //github.com/plataformatec/devise/pull/1147