如何自定义控制器以在 Devise 中注册? [英] How do I customize the controller for registration in Devise?
问题描述
当新用户通过 Devise 注册时,我需要添加一些简单的方法和操作.
I need to add some simple methods and actions when a new user registers through Devise.
我想应用一个通知方法,它会向我发送电子邮件.
I want to apply a notify method which will send an email to me.
我想使用 act_as_network 传递会话值并将新注册连接到邀请他们的人.
I want to use acts_as_network to pass a session value and connect the new register to the person who invited them.
我如何自定义,我查看了文档,但我并不完全清楚我需要做什么....谢谢!
How do I customize, I looked at the docs, but I'm not entirely clear what I need to do....thanks!
推荐答案
这就是我要覆盖设计注册控制器的操作.我需要捕获在注册新用户时可能会抛出的异常,但您可以应用相同的技术来自定义您的注册逻辑.
This is what I'm doing to override the Devise Registrations controller. I needed to catch an exception that can potentially be thrown when registering a new User but you can apply the same technique to customize your registration logic.
app/controllers/devise/custom/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
.
Note that I created a new devise/custom
directory structure under app/controllers
where I placed my customized version of the RegistrationsController. As a result you'll need to move your devise registrations views from app/views/devise/registrations
to app/views/devise/custom/registrations
.
另请注意,覆盖设计注册控制器允许您自定义其他一些内容,例如成功注册后将用户重定向到何处.这是通过覆盖 after_sign_up_path_for
和/或 after_inactive_sign_up_path_for
方法来完成的.
Also note that overriding the devise Registrations controller allows you to customize a few other things such as where to redirect a user after a successful registration. This is done by overriding the after_sign_up_path_for
and/or after_inactive_sign_up_path_for
methods.
routes.rb
devise_for :users,
:controllers => { :registrations => "devise/custom/registrations" }
此帖子可能会提供您可能感兴趣的其他信息.
This post may offer additional information you might be interested in.
这篇关于如何自定义控制器以在 Devise 中注册?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!