In my rails app using Devise, when an unsigned in user visits root they are redirected to /users/sign_in instead of the root path; root path works fine with signed in users. The same thing happens when I click the navbar logo which is a link with the home_path which is root, I am redirected to /users/sign_in just like when visiting root. Here are my routes:
在使用Devise的rails应用程序中,当未签名的用户访问root时,他们将被重定向到/ users / sign_in而不是根路径; root path可以在登录用户中正常工作。当我点击navbar徽标时会发生同样的事情,该徽标是与root_的home_path的链接,我被重定向到/ users / sign_in就像访问root时一样。这是我的路线:
get 'home' => 'static_pages#home', as: "home"
devise_for :users, :controllers => { registrations: 'registrations' }
root 'static_pages#home'
The link that takes me to /users/sign_in instead of home_path/root_path in navbar template:
在navbar模板中将我带到/ users / sign_in而不是home_path / root_path的链接:
<a class="navbar-brand" href="<%= home_path %>">Brand</a>
I am using angular on the front end so this is what the path looks like when a signed in user visits root:
我在前端使用angular,所以当登录用户访问root时,这就是路径的样子:
http://localhost:3000/home#/
If if I manually change the url to /home#/ or /home I am still redirected to users/sign_in.
如果我手动将网址更改为/ home#/或/ home,我仍然会重定向到users / sign_in。
I can't think of any other information to include. Is this just default behavior by Devise that I need to override? Can't seem to find anything on automatic redirecting in the docs. Thanks for the help.
我想不出要包括的任何其他信息。这只是我需要覆盖的Devise的默认行为吗?似乎无法在文档中自动重定向找到任何内容。谢谢您的帮助。
Application controller:
应用控制器:
class ApplicationController < ActionController::Base
before_action :authenticate_user!
protect_from_forgery with: :exception
def after_sign_in_path_for(user)
home_path
end
end
static_pages controller:
static_pages控制器:
class StaticPagesController < ApplicationController
def home
end
end
2 个解决方案
#1
0
In your ApplicationController, which StaticPagesController subclasses, you set before_action :authenticate_user!
, which will be triggered for all actions, which attempts to authenticate a User.
在您的ApplicationController中,StaticPagesController子类,您设置before_action:authenticate_user!,它将针对尝试验证用户的所有操作触发。
This means, that when a User isn't signed in, the authentication check will fail, and you will be redirected to the User sign in route.
这意味着,当用户未登录时,身份验证检查将失败,您将被重定向到用户登录路由。
#2
1
This is because the helper authenticate_user!
only allows signed in users to access the web pages. You need to add skip_before_action :authenticate_user!
to the statics_controller
in order to let the non-loggedin users to access the home
page
这是因为帮助者authenticate_user!只允许登录用户访问网页。您需要添加skip_before_action:authenticate_user!到statics_controller,以便让非登录用户访问主页
class StaticPagesController < ApplicationController
skip_before_action :authenticate_user!
def home
end
end
#1
0
In your ApplicationController, which StaticPagesController subclasses, you set before_action :authenticate_user!
, which will be triggered for all actions, which attempts to authenticate a User.
在您的ApplicationController中,StaticPagesController子类,您设置before_action:authenticate_user!,它将针对尝试验证用户的所有操作触发。
This means, that when a User isn't signed in, the authentication check will fail, and you will be redirected to the User sign in route.
这意味着,当用户未登录时,身份验证检查将失败,您将被重定向到用户登录路由。
#2
1
This is because the helper authenticate_user!
only allows signed in users to access the web pages. You need to add skip_before_action :authenticate_user!
to the statics_controller
in order to let the non-loggedin users to access the home
page
这是因为帮助者authenticate_user!只允许登录用户访问网页。您需要添加skip_before_action:authenticate_user!到statics_controller,以便让非登录用户访问主页
class StaticPagesController < ApplicationController
skip_before_action :authenticate_user!
def home
end
end