在我的应用程序中,我有 root_path root 'home#mainPage'
,虽然用户已登录,但他可以访问我不想要的http://localhost:3000/
。所以我按照https://stackoverflow.com/a/8739874/3786657回答添加此功能,我得到了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'
LIB / 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
配置/ application.rb中
config.autoload_paths << Rails.root.join('lib')
答案 0 :(得分:1)
user_signed_in?
中定义的ApplicationHelper
方法不适用于AuthenticatedUser
。由于您可以从请求中获取会话,因此我将直接在AuthenticatedUser
:
class AuthenticatedUser
def self.matches?(request)
!!request.session[:user_id]
end
end