如何解决如何自定义控制器以在Devise中注册?
| 当新用户通过Devise注册时,我需要添加一些简单的方法和操作。 我想应用一个通知方法,它将向我发送电子邮件。 我想使用acts_as_network传递会话值,并将新寄存器连接到邀请它们的人。 我如何自定义,我看了看文档,但是我还不清楚我需要做什么...。谢谢!解决方法
这就是我要覆盖“设备注册”控制器的目的。我需要捕获一个在注册新用户时可能引发的异常,但是您可以应用相同的技术来自定义注册逻辑。
应用程序/控制器/设计/自定义/registrations_controller.rb
class Devise::Custom::RegistrationsController < Devise::RegistrationsController
def new
super # no customization,simply call the devise implementation
end
def create
begin
super # this calls Devise::RegistrationsController#create
rescue MyApp::Error => e
e.errors.each { |error| resource.errors.add :base,error }
clean_up_passwords(resource)
respond_with_navigational(resource) { render_with_scope :new }
end
end
def update
super # no customization,simply call the devise implementation
end
protected
def after_sign_up_path_for(resource)
new_user_session_path
end
def after_inactive_sign_up_path_for(resource)
new_user_session_path
end
end
请注意,我在app/controllers
下创建了一个新的devise/custom
目录结构,在其中放置了我的自定义版本的RegistrationsController。结果,您需要将设计注册视图从app/views/devise/registrations
移到app/views/devise/custom/registrations
。
还请注意,通过覆盖devise注册控制器,您可以自定义其他一些内容,例如在成功注册后将用户重定向到何处。这是通过覆盖after_sign_up_path_for
和/或after_inactive_sign_up_path_for
方法来完成的。
routes.rb
devise_for :users,:controllers => { :registrations => \"devise/custom/registrations\" }
这篇文章可能会提供您可能感兴趣的其他信息。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。