2015-03-13 18 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

相關問題