覆盖Devise的after_sign_up_path_for方法不起作用。

30 浏览
0 Comments

覆盖Devise的after_sign_up_path_for方法不起作用。

在路由中,我将根路径指向 \"home#index\",但当我尝试用 after_sign_up_path_for 来覆盖它时,无论我是登录还是注册,都会重定向到根路径。我已经尝试在 devise 的子类控制器和 application_controller 中都放置了它,但没有生效。我需要在这里做什么? \n应用控制器\n

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

\n注册控制器\n

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

\n路由\n

root :to => "home#index"

0
0 Comments

这个问题的出现是因为作者忘记声明他正在重写devise的注册控制器。在作者的情况下,他正在使用带有:user资源的devise,所以他在routes.rb中添加了以下内容:

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

在添加这行代码之后,作者在after_inactive_sign_up_path_for中指定的重定向起作用了。

解决这个问题的方法是在routes.rb中声明重写的控制器。作者在这个问题中提到了一个更全面的讨论Override devise registrations controller,其中提供了其他声明重写的方法。

0
0 Comments

问题的原因是使用自定义的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

0
0 Comments

问题的原因是当使用Confirmable模块启用时,需要重写after_inactive_sign_up_path_for方法,因为新的注册需要确认后才能被激活。而如果Confirmable模块启用时,after_sign_up_path_for方法似乎不会被调用。

解决方法是在自己的controller中声明registrations,并在其中插入after_inactive_sign_up_path_for方法。

代码示例:

class RegistrationsController < Devise::RegistrationsController
  protected
  def after_inactive_sign_up_path_for(resource)
    # your custom logic here
  end
end

以上就是关于overriding devise after_sign_up_path_for not working问题的原因和解决方法。

0