[英]Redirect root_path in rails for already signed_in user without using Devise
在我的应用程序中,我有root_path root 'home#mainPage'
,虽然用户已登录,但他可以访问我不想要的http://localhost:3000/
。 所以我正在关注https://stackoverflow.com/a/8739874/3786657答案以添加此功能,但我得到了undefined method user_signed_in? for AuthenticatedUser:Class
undefined method user_signed_in? for AuthenticatedUser:Class
。 我正在使用 Rails 4.2.0
我的路线:
constraints(AuthenticatedUser) do
root :to => "users#show", as: :authenticated
end
root 'home#mainPage'
库/authenticated_user.rb:
class AuthenticatedUser
def self.matches?(request)
user_signed_in?
end
end
application_helper.rb
module ApplicationHelper
def user_signed_in?
!!session[:user_id]
end
def current_user
User.find(session[:user_id])
end
end
配置/应用程序.rb
config.autoload_paths << Rails.root.join('lib')
user_signed_in?
ApplicationHelper
定义的方法对AuthenticatedUser
不可用。 由于您可以从请求中获取会话,因此我将直接在AuthenticatedUser
检查它:
class AuthenticatedUser
def self.matches?(request)
!!request.session[:user_id]
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.