繁体   English   中英

Rails设计 - 为登录的用户和匿名用户定义单独的根路径

[英]Rails Devise - Define separate root paths for signed in user and anonymous user

我希望signed_in用户的root_pathdashboard ,而未登录用户的用户是index

我正在使用Devise,我知道有一个帮助user_signed_in? 但我不明白如何将它用于此目的。

我该如何工作?

你可以用这个:

unauthenticated :user do
  root :to => 'main#index'
end

authenticated :user do
  # Rails 3 syntax
  # root :to => "main#dashboard"   
  # Rails 4 requires the 'as' option to give it a unique name
  root :to => "main#dashboard", :as => "authenticated_root"
end

这是Devise为重新生成经过身份验证的用户提供的机制。

root :to => 'main#index' config/routes.rb root :to => 'main#index'行是Rails在首次创建应用程序时在config/routes.rb放置的标准行。 您可以将其包装在unauthenticated :user do .. end块中,以确保它适用于未登录的用户。

这为所有用户提供了明确的根路由,具体取决于他们是否已登录。

您可以在应用程序控制器( application_controller.rb )中执行以下操作

protected
def authenticate_user!
if user_signed_in?
  # navigate the user to dashboard
else
  # redirect to index
end

然后可以使用before_filter从其他控制器调用此方法示例:

class SomeController < ApplicationController
  before_filter :authenticate_user!
end

暂无
暂无

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

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