js1*_*111 41 ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1
如何在设计中创建后确认重定向?
在我第一次添加confirmation module自定义after_sign_up_path工作正常之前,login/signup但现在当我单击电子邮件中的确认链接时,它会重定向到我为登录后路径设置的路径(用户配置文件).
我的目标是创建表单向导和"入门"页面以收集其他信息.显而易见的警告是,这种重定向只会在确认后发生一次.
我已经尝试了一些已经在堆栈上发布的其他解决方案,但它们似乎都不再工作了..
Mic*_*elZ 127
实现这一目标的一种不那么具有侵入性的方式可能只是重写after_confirmation_path_for方法Devise::ConfirmationsController.
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
Run Code Online (Sandbox Code Playgroud)
在config/routes.rb,添加此行,以便Devise将使用您的自定义ConfirmationsController.假设Devise在users桌面上运行(您可以编辑以匹配您的).
devise_for :users, controllers: { confirmations: 'confirmations' }
Run Code Online (Sandbox Code Playgroud)
重新启动Web服务器,您应该拥有它.
nie*_*els 19
你检查过Devise wiki吗?它解释了如何做到这一点,after_signup_path_for作为你的案例中定义的路径.
来自维基:
创建一个新的控制器RegistrationsController:
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
'/an/example/path'
end
end
Run Code Online (Sandbox Code Playgroud)
并添加一个使用它的路线:
devise_for :users, :controllers => { :registrations => "registrations" }
Run Code Online (Sandbox Code Playgroud)
Lee*_*ith 19
基本上,您想要更改Devise ConfirmationsController的这一行:
所以这意味着你需要覆盖show动作.只需将show动作中"if"语句的快乐路径修改为您心中的内容:
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
Run Code Online (Sandbox Code Playgroud)
并为它设定了一个范围路线(我将视图和操作放在注册控制器中,但您可以将其更改为任何内容):
devise_for :users, controllers: { confirmations: 'confirmations' }
devise_scope :user do
get '/confirmation-getting-started' => 'registrations#getting_started', as: 'confirmation_getting_started'
end
Run Code Online (Sandbox Code Playgroud)
默认的show动作是指受保护的after_confirmation_path_for方法,因此作为另一个选项,您可以只修改该方法返回的内容.
| 归档时间: |
|
| 查看次数: |
24030 次 |
| 最近记录: |