我正在使用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 :(得分:2)
在任何root用户的控制器操作中,您只需说:
if current_user
redirect_to user_objects_path #look up the rails helper
end
答案 1 :(得分:0)
constraints
上的documentation建议将自定义约束类放到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]