[英]Using rails, devise and cancan, a signed in user can access the sign in and sign up paths
[英]Rails Devise - Define separate root paths for signed in user and anonymous user
我希望signed_in用户的root_path
是dashboard
,而未登录用户的用户是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.