覆盖设计after_sign_up_path_for不起作用

在路由中,我的根路径指向"home#index" #index "home#index"但是当我尝试覆盖它时,after_sign_up_path_for在我登录或注册时会将我重定向到根路径。 我试图将它放在设计子类控制器和application_controller中,但它没有用。 我需要做什么?

应用控制器

 class ApplicationController < ActionController::Base protect_from_forgery def after_sign_up_path_for(resource) show_cities_path(resource) end end 

登记控制员

 class RegistrationsController < ApplicationController def after_sign_up_path_for(resource) show_cities_path(resource) end end 

路线

 root :to => "home#index" 

您是否通过执行rake routes检查了show_cities_path是否存在? 可能值得一看https://github.com/plataformatec/devise/wiki/How-To:-Change-the-redirect-path-after-destroying-a-session-ie-signing-out

如果您还启用了可确认模块,则必须覆盖after_inactive_sign_up_path_for因为新注册处于“非活动状态”,直到确认为止。 当Confirmable处于活动状态时,似乎没有调用after_sign_up_path_for

虽然我迟到了比赛,但我遇到了这个问题并且无法找到解决方案。

如果您使用自己的RegistrationsController来自定义Devise,那么您需要将after_sign_up_path_for(resource)方法添加到该控制器而不是ApplicationController。

在registrations_controller.rb中:

 private def after_sign_up_path_for(resource) new_page_path end 

https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb

我一直在努力解决这个问题,直到意识到我已经忘记宣布我已经超越了设计注册控制器。 在我的情况下,我正在使用:用户资源设计,所以我将其添加到routes.rb:

 devise_for :users, :controllers => {:registrations => "registrations"} 

之后,我在after_inactive_sign_up_path_for中指定的重定向工作。

覆盖设计注册控制器有关于此主题的更完整的讨论,以及声明覆盖的替代方法。

我刚刚吹了2个小时,但LiveReload是我的问题。 我被重定向成功,但LiveReload正在接受develop.sqllite上的更改并覆盖请求。

实际上,我们可以查看设计的源代码来解决问题,这很容易。

devise-3.4.1 $ vim app/controllers/devise/registrations_controller.rb

  # POST /resource def create build_resource(sign_up_params) resource_saved = resource.save yield resource if block_given? if resource_saved if resource.active_for_authentication? set_flash_message :notice, :signed_up if is_flashing_format? sign_up(resource_name, resource) respond_with resource, location: after_sign_up_path_for(resource) else set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format? expire_data_after_sign_in! respond_with resource, location: after_inactive_sign_up_path_for(resource) end else clean_up_passwords resource @validatable = devise_mapping.validatable? if @validatable @minimum_password_length = resource_class.password_length.min end respond_with resource end end 

如代码所示:

  if resource.active_for_authentication? ... respond_with resource, location: after_sign_up_path_for(resource) else ... respond_with resource, location: after_inactive_sign_up_path_for(resource) 

如果在Rails 5.2中使用OmniAuth自定义回调控制器:

在我的特定情况下,后注册路径不起作用:但我使用OmniAuth与自定义回调控制器,它调用after_sign_in_path_for而不是前者:

def google_oauth2 @user = User.from_omniauth(request.env [“omniauth.auth”])

 if @user.persisted? # Here's the guilty line your honour: sign_in_and_redirect @user, event: :authentication #this will throw if @user is not activated set_flash_message(:notice, :success, kind: "Google") if is_navigational_format? else session["devise.google_oauth2_data"] = request.env["omniauth.auth"] redirect_to new_user_registration_url end 

结束

……..并且sign_in_and_redirect路径重定向到after_sign_in_path_for方法。 所以我为会话生成了一个新的设计控制器,并简单地覆盖了该方法。 问题解决了!