Ruby on rails 如果用户登录,尝试重定向用户时遇到问题
我使用的是Rails 4.2.5。如果用户已登录,我希望在他们访问时重定向他们。因此,我将其添加到我的config/routes.rb文件中Ruby on rails 如果用户登录,尝试重定向用户时遇到问题,ruby-on-rails,authentication,redirect,routes,Ruby On Rails,Authentication,Redirect,Routes,我使用的是Rails 4.2.5。如果用户已登录,我希望在他们访问时重定向他们。因此,我将其添加到我的config/routes.rb文件中 constraints(AuthenticatedUser) do root :to => "user_objects" end root 'pages#index' 然后我将这个文件保存在lib/authenticated_user.rb中 class AuthenticatedUser def self.matches?
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
不幸的是,当我登录并访问时,会出现此错误
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'
在控制器操作中,无论根是什么,您都可以说:
if current_user
redirect_to user_objects_path #look up the rails helper
end
在控制器操作中,无论根是什么,您都可以说:
if current_user
redirect_to user_objects_path #look up the rails helper
end
关于constraints
的建议将自定义约束类放入lib/constraints
无论如何,要在路由中识别类
,应该自动加载该类。将约束所在的目录添加到application.rb中的自动加载目录列表中:
# config/application.rb
# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W[...
#{config.root}/lib/constraints]
关于constraints
的建议将自定义约束类放入lib/constraints
无论如何,要在路由中识别类
,应该自动加载该类。将约束所在的目录添加到application.rb中的自动加载目录列表中:
# config/application.rb
# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W[...
#{config.root}/lib/constraints]
当你rake routes
时,object\u用户的助手方法是什么?尝试剪切和粘贴时有点混乱,但我在我的config/routes.rb文件中添加了一个非常基本的文件。当你rake routes
时object\u用户的助手方法是什么?尝试剪切和粘贴时有点混乱,但是我需要我的config/routes.rb文件,这是一个非常基本的文件?对于rake路由,我为根页面设置了“root GET/pages#index”,它将进入页面控制器的索引操作,这将进入我的应用程序#控制器文件吗?对于rake路由,我为根页面设置了“root GET/pages#index”,它将进入页面控制器的索引操作。我将该文件移动到约束目录并添加了YRU代码,但当我访问时,它仍然试图呈现页面#index,而不是重定向到“root:to=>“user#objects”。我明白了,我承认我被“uninitialized constant”错误,我忘了跟踪您的根问题:)。我又想了想,我也会选择@toddmetheny的方法。我将该文件移动到约束目录并添加了YRU代码,但当我访问时,它仍然试图呈现页面#索引,而不是重定向到“root:to=>”用户对象“'我明白了,我承认我被“未初始化常量”错误分心了,以至于忘了关注您的根问题:)。再想一想,我也会选择@toddmetheny的方法。