2016-03-23 38 views
0

我正在使用Rails 4.2.5。如果用戶登錄,我想重定向他們,如果他們訪問http://localhost:3000/http://localhost:3000/user_objects。所以,我已經加入這個我的config/routes.rb中文件在嘗試重定向用戶時遇到問題(如果他們已登錄)

constraints(AuthenticatedUser) do 
    root :to => "user_objects" 
    end 
    root 'pages#index' 

然後我在LIB/authenticated_user.rb這個文件......

class AuthenticatedUser 
    def self.matches?(request) 
    user_signed_in? 
    end 
end 

不幸的是,當我登錄,我訪問http://localhost:3000/,我得到這個錯誤

NameError 

uninitialized constant AuthenticatedUser 

任何想法如何,我可以,如果有人登錄的用戶重定向到建議的網頁?

編輯:我的config/routes.rb中文件如下...

resources :user_objects, except: :update do 
    member do 
     patch :create 
     put :create 
    end 
    get :find_by_user_object_and_day, on: :collection 
    get :find_totals, on: :collection 
    end 

    get "users/edit" => "users#edit" 
    resources :users 

    root 'pages#index' 

    get '/auth/:provider/callback', to: 'sessions#create' 
    get '/logout', to: 'sessions#destroy' 
    delete '/logout', to: 'sessions#destroy' 
+0

什麼是對object_user輔助方法,當你'耙routes'? – toddmetheny

+0

它試圖剪切和粘貼它有點混亂,但我想我的config/routes.rb文件,這是非常基本的。 – Dave

回答

2

在無論何種根是控制器動作,你可以說:

if current_user 
    redirect_to user_objects_path #look up the rails helper  
end 
+0

這會在我的application_controller文件?對於耙路徑,我有根頁面的「根GET /頁面#索引」 – Dave

+0

它將在頁面控制器的索引動作 – toddmetheny

0

documentation on constraints建議將自定義約束類別放置到lib/constraints

無論如何,對於需要識別的類routes您應該自動加載。其中約束駐留添加目錄自動加載目錄中application.rb名單:

# config/application.rb 
# Custom directories with classes and modules you want to be autoloadable. 
config.autoload_paths += %W[... 
          #{config.root}/lib/constraints] 
+0

我將該文件移動到約束目錄並添加yoru代碼,但是當我訪問http: // localhost:3000 /,它仍然嘗試渲染頁面#索引,而不是重定向到「root:to =>」user_objects「。 – Dave

+0

我明白了,我承認我被「未初始化的常量」錯誤所分心,因此我忘記了遵循根本問題:)。再次想到,我也會選擇@ toddmetheny的方法。 – BoraMa

相關問題