如何在Devise中自定义控制器以进行注册?

Tim*_* T. 5 controller ruby-on-rails devise

当新用户通过Devise注册时,我需要添加一些简单的方法和操作.

我想申请一个发送电子邮件给我的通知方法.

我想使用acts_as_network传递会话值并将新注册表连接到邀请它们的人.

我如何定制,我查看了文档,但我并不完全清楚我需要做什么....谢谢!

mbr*_*ing 15

这就是我正在做的覆盖Devise Registrations控制器.我需要捕获在注册新用户时可能引发的异常,但您可以应用相同的技术来自定义注册逻辑.

应用程序/控制器/设计/定制/ 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
Run Code Online (Sandbox Code Playgroud)

请注意,我在devise/custom下面app/controllers放置了我的自定义版本的RegistrationsController ,创建了一个新的目录结构.因此,您需要将设计注册视图从中移动app/views/devise/registrationsapp/views/devise/custom/registrations.

另请注意,覆盖设备注册控制器允许您自定义一些其他内容,例如在成功注册后重定向用户的位置.这是通过覆盖after_sign_up_path_for和/或after_inactive_sign_up_path_for方法来完成的.

的routes.rb

  devise_for :users,
             :controllers => { :registrations => "devise/custom/registrations" }
Run Code Online (Sandbox Code Playgroud)

这篇文章可能会提供您可能感兴趣的其他信息.