ruby-on-rails – Rails设计after_sign_in_path_for(资源)方法无
我想将非活动用户重定向到注册路径以收集一些信息.以下是我采取的两种方法但两种方法都没有效果:
我按如下方式覆盖了设计after_sign_in_path方法(在application_controller.rb中): def after_sign_in_path_for(resource) debugger if(account_active) return root_path; else return edit_user_registration_path(resource) end end 当我将代码连接到调试器时,我看到devise确实调用了after_sign_in_path_for.此外,此调用生成了正确的URL: (rdb:2) after_sign_in_path_for(resource) "/users/edit.1" 但是,当我查看服务器日志时,在任何情况下都不会尝试重定向到“/users/edit.1”. 我已经尝试将上述方法移动到application_helper.rb,session_controller.rb(通过扩展Devise :: SessionController)和session_helper.rb 问题是设计确实调用此方法来检索URL,但它从不尝试重定向.我检查了Web服务器日志,并直接设计到user_root url. 以下是routes.rb的相关设计配置: devise_for :users do resource :registration,only: [:new,:create,:edit,:update],path: 'users',path_names: { new: 'sign_up' },controller: 'devise/registrations',as: :user_registration do get :cancel end root :to => "home#index" end match '/user' => "products#index",:as => 'user_root' 关于我应该尝试什么的任何建议? 谢谢, Tabrez 解决方法
您确定要重定向到/users/edit.1吗? Rails会选择它,就像你试图访问1 mime-type而不是html一样.
用户注册路径不需要id,因为它始终属于当前登录的用户.这应该足够了: def after_sign_in_path_for(resource) if account_active root_path else edit_user_registration_path end end 此外,将它放在ApplicationController中是正确的位置.如果你有自己的会话控制器,比如继承自Devise :: SessionsController的Users :: SessionsController,那么它也可以进入. 因此,account_active方法不会按照您的想法执行,或者您已经搞砸了路径文件.尝试在路由中使用更多的vanilla配置,看看是否是这种情况: devise_for :users PS.作为一个完全无关的附注:请尝试使用Ruby编码约定,例如在不需要时不使用分号,if语句中没有括号,2个空格缩进和没有不需要的返回语句. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |