gpt4 book ai didi

ruby-on-rails - Rails 4 Devise,after_sign_in_path_for(resource) 总是重定向到模型的显示操作

转载 作者:数据小太阳 更新时间:2023-10-29 09:03:29 25 4
gpt4 key购买 nike

我坚持使用设计的 after_sign_in_path_for 方法,这就是事情......我有两个模型 UsersAdminUsers 我正在使用 active_admin gem

我的routes.rb 文件,看起来像这样:

devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
scope "(:locale)", locale: /es|en/ do
devise_for :users, path_names: { sign_in: 'login', sign_out: 'logout',
password: 'password', confirmation: 'confirmation', unlock: 'unlock',
registration: 'registration', sign_up: 'sign_up' }
devise_scope :user do
get "login", to: "devise/sessions#new"
end
get 'dashboard/index'
end

好的...在我的 application_controller.rb 中,我尝试了以下代码:

def after_sign_in_path_for(resource)
case resource
when AdminUser
#admin_root_path
'/admin/dashboard'
puts'in admin_root_path'
when User
#dashboard_index_path
'/dashboard/index'
puts'in dashboard_index_path'
else super
end
puts 'resource Of AplicationController: ' + resource.class.to_s

end

def after_sign_out_path_for(resource_or_scope)
root_path
end

如您所见,如果我的案例 有效,我有一些puts 只是为了检查我的控制台...确实如此,它确实有效,这是我的安慰:我正在尝试以 AdminUser 身份登录到 active_admin 的 admin_root_path

Started POST "/admin/login?locale=es" for ::1 at 2015-04-14 12:46:51 -0400
Processing by ActiveAdmin::Devise::SessionsController#create as HTML
....
....
in admin_root_path
resource of AplicationController: AdminUser
Redirected to http://localhost:3000/admin/users/2?locale=es
Completed 302 Found in 113ms (ActiveRecord: 1.8ms)

如您所见,Devise 将我重定向到 current_admin_user 的显示页面。

如果我尝试以用户身份登录,控制台如下:

Started POST "/es/users/login" for ::1 at 2015-04-14 12:51:18 -0400
Processing by Devise::SessionsController#create as HTML
...
...
in dashboard_index_path
resource of AplicationController: User
Redirected to http://localhost:3000/es/users/2
Completed 302 Found in 106ms (ActiveRecord: 1.4ms)

在这两种情况下,Devise 都在做同样的事情...重定向到每个模型的显示操作,我已经尝试将此代码放在自定义 registrations_controller.rb 中,并且相同...也许我遗漏了一些明显的东西,我不是 Devise 方面的专家,有人知道我做错了什么吗?

rake routes命令的相关结果

....
....
edit_admin_user_password GET /admin/password/edit(.:format) active_admin/devise/passwords#edit

admin_root GET /admin(.:format) admin/dashboard#index
....
....
admin_dashboard GET /admin/dashboard(.:format) admin/dashboard#index
....
....
dashboard_index GET (/:locale)/dashboard/index(.:format) dashboard#index {:locale=>/es|en/}
root GET / visitors#index

最佳答案

请试试这个:

def after_sign_in_path_for(resource)
if resource.class == AdminUser
admin_root_path
elsif resource.class == User
dashboard_index_path
end
end

需要注意两件事:我使用 resourse.class 我认为这是问题中的错字,而不是代码中的错字(因为它会打印 puts)。其次,after_sign_in_path 必须返回一个 url,代码的最后一个操作是返回 nilputs

你也可以试试:

def after_sign_in_path_for(resource)
dashboard_index_path
end

第一次看到 after_sign_in_path_for 工作。并以此为起点继续前行。

如果这一切都不起作用,请发布 sign_inlogin 路由。

编辑:阅读您的帖子后,我意识到 case 不起作用(错误:有效),因为 case 与 ===resource 一起使用.class === AdminUser 总是返回 false(是不同的对象)。您必须请求 resource.class == AdminUserresource.is_a?(AdminUser)。更多案例匹配here .我还更新了 after_sign_in_path 方法。

编辑 2在阅读@CJBrew 的评论和 irb 的这些实验之后:

(main) > pp = Person.first
=> "#<Person id: 1 ..."
(main) > pp === Person
=> false
(main) > Person === pp
=> true
(main) > case pp
(main) | when Person
(main) | 'yes'
(main) | end
=> "yes"

我可以说这段代码一定能正常工作:

def after_sign_in_path_for(resource)
case resource
when User
root_path
when AdminUser
admin_contexts_path
end
end

因为 case 使用 === 并将 when 值作为对象,将 case 值作为参数。在这种情况下,第一个 when 子句用 User.===(resource)User === resource 测试,第二个用 AdminUser.= ==(资源)。由于 UserAdminUser 是类对象,它们使用 Module#=== method .这里重要的是要注意这个方法是不可交换的,它是一个类方法,它接受任何对象作为参数,如果参数是该类的实例则返回 true。

关于ruby-on-rails - Rails 4 Devise,after_sign_in_path_for(resource) 总是重定向到模型的显示操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29633263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com