确认后deviseredirect

如何在devise中创build一个确认后redirect?

在我添加confirmation module之前,自定义的after_sign_up_path在第一次login/signup时工作正常,但是现在,当我单击电子邮件中的确认链接时,它将redirect到我为后loginpath(用户configuration文件)设置的path。

我的目标是创build一个表单向导和“入门”页面来收集更多信息。 显而易见的警告是,这个redirect只会在确认后发生一次。

我已经尝试了一些其他的解决scheme已经张贴在堆栈上,但他们似乎没有任何工作了..

本质上,你想改变devise确认控制器的这一行:

https://github.com/plataformatec/devise/blob/master/app/controllers/devise/confirmations_controller.rb#L25

所以这意味着你需要重写show动作。 只要修改show action中那个“if”语句的快乐path到你心中的内容即可:

 class ConfirmationsController < Devise::ConfirmationsController def new super end def create super end def show self.resource = resource_class.confirm_by_token(params[:confirmation_token]) if resource.errors.empty? set_flash_message(:notice, :confirmed) if is_navigational_format? sign_in(resource_name, resource) respond_with_navigational(resource){ redirect_to confirmation_getting_started_path } else respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render_with_scope :new } end end end 

和它的作用域路线(我把视图和行动注册控制器,但你可以改变任何):

 devise_for :users, controllers: { confirmations: 'confirmations' } devise_scope :user do get '/confirmation-getting-started' => 'registrations#getting_started', as: 'confirmation_getting_started' end 

默认的show动作引用了受保护的after_confirmation_path_for方法,所以作为另一个选项,你可以修改那个方法返回的内容。

实现这一点的干扰较小的方法可能只是覆盖Devise::ConfirmationsControllerafter_confirmation_path_for方法。

app/controllers目录下创build一个新的confirmations_controller.rb

 class ConfirmationsController < Devise::ConfirmationsController private def after_confirmation_path_for(resource_name, resource) your_new_after_confirmation_path end end 

config/routes.rb ,添加这一行以便Devise将使用您的自定义ConfirmationsController 。 这假定Devise在users表上操作(您可以编辑以匹配您的用户)。

 devise_for :users, controllers: { confirmations: 'confirmations' } 

重新启动Web服务器,你应该拥有它。

你检查了Devise wiki吗? 它解释了如何做到这一点, after_signup_path_for是你的情况下定义的path。

来自wiki:

创build一个新的控制器RegistrationsController:

 class RegistrationsController < Devise::RegistrationsController protected def after_sign_up_path_for(resource) '/an/example/path' end end 

并添加一条路线来使用它:

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

我只是通过所有这一切,没有其他的答案工作(2015-04-09与devise3.4.1)。

我想要的是在注册后,用户通过关于确认邮件的消息被redirect到login页面。 要做到这一点,这是我必须做的:

 class RegistrationsController < Devise::RegistrationsController protected # This is the method that is needed to control the after_sign_up_path # when using confirmable. def after_inactive_sign_up_path_for(resource) new_user_session_path end end 

编辑:我刚刚发现下面的评论,这将发送我到哪里我需要更快。

这里是参考after_inactive_sign_up_path_for提到Niels:devise维基 – marrossa 11年11月13日在3:38

由@Lee Smith给出的解决scheme是完美的,但是我希望添加一点点,那就是我们不需要在为这种情况重写Devise confirmations控制器的同时添加新的和创build的动作

像:

 class ConfirmationsController < Devise::ConfirmationsController def show self.resource = resource_class.confirm_by_token(params[:confirmation_token]) if resource.errors.empty? set_flash_message(:notice, :confirmed) if is_navigational_format? sign_in(resource_name, resource) respond_with_navigational(resource){ redirect_to your_desired_redirect_path } else respond_with_navigational(resource.errors, status: :unprocessable_entity){ render_with_scope :new } end end end 

然后在路由文件中,为确认控制器添加路由。

  devise_for :users, controllers: { confirmations: "confirmations" } 

在使用集成在rails应用程序中的refinerycms时,也必须configurationconfirmation_path