简体   繁体   English

如何使用Devise gem创建用户的根路径路由?

[英]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 ,您还有其他两种选择:

  1. Use unauthenticated_path 使用unauthenticated_path
  2. 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.

相关问题 如何使用devise gem for Rails自定义用户注册路线? - How do I customize the user registration route with the devise gem for Rails? 登录后如何将用户重定向到其他路径? (与Devise一起) - How do I redirect users to a different path upon signing in? (with Devise) 使用Devise gem,如何显示带有评论的用户名? - Using Devise gem, how do i display username with comment? 不使用 Devise 为登录用户指定不同的根路径 - Specify a different root path for logged in users without using Devise 用户的不同&#39;/&#39;根路径,具体取决于它们是否经过身份验证(使用设计) - Different '/' root path for users depending if they are authenticated (using devise) 无法使用Devise gem在Rails应用中注销,没有路线与/ users / sign_out匹配 - Unable to sign out in a Rails app, using Devise gem, no route matches /users/sign_out 当我使用devise gem进行注册/登录时如何使用代码创建用户 - How to create a user using code when i am using devise gem for Sign up / Sign in 如何使用Devise根据角色重定向用户的主(根)路径? - How can I redirect a user's home (root) path based on their role using Devise? 如何使用 Wicked gem 创建路线并使用路径以多步形式进入第一步? - How can I create a route and use a path to enter the first step in a mulistep form with the Wicked gem? 使用devise-guests gem时如何添加回调? - How do I add callbacks when using the devise-guests gem?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM