[英]How do I create the users root path route using the Devise gem?
I did a search and found this question on SO, but the accepted answer doesn't seem to be working for me. 我进行了搜索,并在SO上找到了这个问题 ,但是可接受的答案似乎对我没有用。 Basically, the Divise wiki says...
基本上,Divise Wiki表示...
After signing in a user, confirming the account or updating the password, Devise will look for a scoped root path to redirect.
登录用户,确认帐户或更新密码后,Devise将查找要重定向的作用域根路径。 For instance, for a :user resource, the user_root_path will be used if it exists, otherwise the default root_path will be used.
例如,对于:user资源,将使用user_root_path(如果存在),否则将使用默认的root_path。
With my amateur knowledge of RoR, I have a devise model called Player, and I created the following statement in my routes.rb file... 利用我对RoR的业余知识,我有了一个名为Player的设计模型,并且在我的route.rb文件中创建了以下语句...
match 'player_root', to: 'pages#play', as: :player_root, via: :all
...but with that, my app always redirects to my root path, instead of the players root path, which I defined above. ...但是,我的应用程序始终重定向到我的根路径,而不是我上面定义的玩家根路径。 What am I doing wrong?
我究竟做错了什么?
Thanks in advance for your wisdom! 在此先感谢您的智慧! Also, I'm using Ruby 2 with Rails 4.
另外,我将Ruby 2与Rails 4一起使用。
As I understand, you are trying to specify root_path
for :players
. 据我了解,您正在尝试为
:players
指定root_path
。
If order to do that, you can use following: 如果要这样做,可以使用以下命令:
authenticated :players do
root to: 'pages#play', as: :authenticated_root
end
This will give you custom root_path for signed in users (players). 这将为登录的用户(玩家)提供自定义的root_path。
Further to Andrey Dieneko
, there are two other options you have: 除了
Andrey Dieneko
,您还有其他两种选择:
- Use
unauthenticated_path
使用
unauthenticated_path
- Use
authenticate_user!
使用
authenticate_user!
in your controller在您的控制器中
The bottom line here is that you may be thinking about incorrectly. 底线是您可能在考虑错误。 You may be trying to work out where to take users if authenticated.
您可能正在尝试确定通过身份验证的用户。 However, you may be better suited to actually using the authentication methods in the controller to test whether the user is logged in, and if not route them to a login page:
但是,您可能更适合实际使用控制器中的身份验证方法来测试用户是否已登录,如果未登录,则将其路由到登录页面:
#config/routes.rb
root to: "players#play"
#app/controllers/players_controller.rb
class PlayersController < ApplicationController
before_action :authenticate_user!
end
This will take a user to the "login" path if they are not logged in. 如果用户未登录,这会将用户带到“登录”路径。
Alternatively, you can use unauthenticated_path
like so: 另外,您可以像这样使用
unauthenticated_path
:
#config/routes.rb
root to: "players#play"
unauthenticated do
root to: "application#landing"
end
-- -
This method will only be best if you have an app like Facebook (IE it has no "landing page" etc) 仅当您拥有Facebook之类的应用程序(例如,它没有“目标网页”等)时,此方法才是最佳方法
I think Andrey's
answer is more apt (especially if you have a landing page) 我认为
Andrey's
答案更为恰当(尤其是如果您有目标网页)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.