Ruby-on-rails 确认后如何使设计重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10926626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make Devise redirect after confirmation
提问by js111
How can I create an after-confirmation redirect in Devise?
如何在 Devise 中创建确认后重定向?
Before I added the confirmation modulethe custom after_sign_up_pathworked fine for the first time login/signupbut now when I click the confirmation link in the email it redirects to the path I set for the after-login path (user profile).
在我第一次添加confirmation module自定义之前,自定义after_sign_up_path工作正常,login/signup但现在当我单击电子邮件中的确认链接时,它会重定向到我为登录后路径(用户配置文件)设置的路径。
My goal is to create a form wizard and "getting started" page to collect additional information. The obvious caveat being that this redirect will only happen one time, upon confirmation.
我的目标是创建一个表单向导和“入门”页面来收集其他信息。明显的警告是,在确认后,此重定向只会发生一次。
I tried some other solutions that have been posted on Stack Overflow but none of them seem to work any longer.
我尝试了 Stack Overflow 上发布的一些其他解决方案,但它们似乎都不再有效。
采纳答案by Lee Smith
Essentially, you want to change around line 25of Devise's ConfirmationsController.
从本质上讲,您希望在Devise 的.25 行附近进行更改ConfirmationsController。
This means you need to override the showaction modifying the "happy path" of that ifstatement in the showaction to your heart's content:
这意味着您需要覆盖show修改操作中该if语句的“快乐路径”的show操作,使其符合您的内心需求:
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
And a scoped route for it (I put the view and action in the registrations controller but you can change it to whatever):
以及它的范围路由(我将视图和操作放在注册控制器中,但您可以将其更改为任何内容):
devise_for :users, controllers: { confirmations: 'confirmations' }
devise_scope :user do
get '/confirmation-getting-started' => 'registrations#getting_started', as: 'confirmation_getting_started'
end
The default showaction is referring to the protected after_confirmation_path_formethod, so as another option, you could just modify what that method returns.
默认show操作是指受保护的after_confirmation_path_for方法,因此作为另一种选择,您可以修改该方法返回的内容。
回答by MichaelZ
A less intrusive way of achieving this might be just overriding the after_confirmation_path_formethod of Devise::ConfirmationsController.
实现这一目标的一个不太侵入性的方式可能只是重写after_confirmation_path_for的方法Devise::ConfirmationsController。
Create a new confirmations_controller.rbin app/controllersdirectory:
confirmations_controller.rb在app/controllers目录中新建一个:
class ConfirmationsController < Devise::ConfirmationsController
private
def after_confirmation_path_for(resource_name, resource)
your_new_after_confirmation_path
end
end
In config/routes.rb, add this line so that Devise will use your custom ConfirmationsController. This assumes Devise operates on userstable (you may edit to match yours).
在中config/routes.rb,添加这一行,以便设计将使用您的自定义ConfirmationsController. 这假设设计在users表上操作(您可以编辑以匹配您的)。
devise_for :users, controllers: { confirmations: 'confirmations' }
Restart the web server, and you should have it.
重新启动 Web 服务器,您应该拥有它。
回答by niels
Have you checked the Devise wiki? It explains how to do this, with the after_signup_path_forbeing the path to define in your case.
你检查过设计维基吗?它解释了如何执行此操作,并after_signup_path_for在您的情况下定义路径。
From the wiki:
来自维基:
Make a new controller "registrations_controller.rb" and customize the appropriate method:
class RegistrationsController < Devise::RegistrationsController protected def after_sign_up_path_for(resource) '/an/example/path' end end
创建一个新的控制器“registrations_controller.rb”并自定义相应的方法:
class RegistrationsController < Devise::RegistrationsController protected def after_sign_up_path_for(resource) '/an/example/path' end end
Then add a route to use it:
然后添加一个路由来使用它:
Modify config/routes.rb to use the new controller
devise_for :users, :controllers => { :registrations => "registrations" }
修改 config/routes.rb 以使用新的控制器
devise_for :users, :controllers => { :registrations => "registrations" }
回答by Md. Mahmudur Rahman
The solution given by @Lee Smith is working perfectly but I wish to add a little addition: We don't need to add the newand createactions while overriding the Devise confirmations controller for this case:
@Lee Smith 给出的解决方案运行良好,但我想添加一点:我们不需要在覆盖这种情况下的设计确认控制器时添加new和create操作:
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
Then in the route file, just add routing for the confirmations controller.
然后在路由文件中,为确认控制器添加路由。
devise_for :users, controllers: { confirmations: "confirmations" }
回答by Karl Wilbur
I just went through all of this and none of the other answers worked (2015-04-09 with devise 3.4.1).
我刚刚经历了所有这些,其他答案都没有奏效(2015-04-09 与设计 3.4.1)。
After signup, I wanted the user to be redirected to the login page with a message about a confirmation email. To get that working, here's what I had to do:
注册后,我希望用户被重定向到登录页面,并显示有关确认电子邮件的消息。为了让它工作,这是我必须做的:
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
I just found this comment which would have sent me exactly where I needed to be much sooner.
我刚刚发现了这条评论,它可以准确地将我送到我需要更快到达的地方。
Here is the reference to the after_inactive_sign_up_path_for that mentions Niels: Devise wiki– marrossa Nov 13 '12 at 3:38
以下是对提及 Niels 的 after_inactive_sign_up_path_for 的参考:Devise wiki– marrossa 2012 年 11 月 13 日 3:38
回答by iago
The confirmation_pathalso must be configured while working with refinerycmsintegrated in a rails app
在confirmation_path同时有工作也必须配置refinerycms集成在一个Rails应用程序

