简体   繁体   English

根用户登录页面和未登录用户页面不同

[英]Different page for logged in user and not logged in user at root

I want to show different root page for users in Rails. 我想在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 目前,登录用户可以,但未登录的用户重定向到/ users / sign_in

I want to show static root page and not redirect. 我想显示静态根页面而不重定向。

The answer, suggested by Puneet Goyal will not work in Rails 4. See this . 答案由普尼特戈亚尔建议不会在Rails的工作4.看这个 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'

In your routes.rb : 在您的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 这将确保welcome#index所有通过身份验证的用户使用root_url

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

This answer should work. 这个答案应该起作用。 This was posted on the page Bradley linked. 这被张贴在Bradley链接的页面上。

Put this in your Welcome controller. 将其放在您的Welcome控制器中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM