2015-03-13 64 views
-1

在我的应用程序中,我有root_pathroot 'home#mainPage'虽然用户已登录,但他可以访问我不想要的http://localhost:3000/。所以我跟随https://stackoverflow.com/a/8739874/3786657答案添加此功能,我得到undefined method user_signed_in? for AuthenticatedUser:Class。我使用Rails 4.2.0在rails中重定向root_path用于已经signed_in用户而不使用Devise

我的路线:

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 

配置/ application.rb中

config.autoload_paths << Rails.root.join('lib') 

回答

1

ApplicationHelper中所定义的方法user_signed_in?不可用于AuthenticatedUser。既然你可以从请求的会话我会直接将其签入AuthenticatedUser

class AuthenticatedUser 
    def self.matches?(request) 
    !!request.session[:user_id] 
    end 
end 
+0

它说'找不到用户与“ID” ='现在 – 2015-03-13 19:57:05

+0

那是哪里的错误出现? – infused 2015-03-13 21:29:39

+0

其发生用户控制器'#使用回调来共享操作之间的常见设置或约束。 def set_user @user = User.find(params [:id]) end' – 2015-03-13 21:55:33

相关问题