我有一个创建方法在我的RegistrationsController,它继承自设计::Registrations控制器。它应该调用Stripe,如果客户创建成功,它保存用户并发送确认电子邮件,这是由设计中的"#create"处理的。如果对Stripe的调用失败,它应该设置一个flash,而不是保存用户或发送电子邮件,即抑制设计的"创建"方法。如果对Stripe的调用成功,该方法可以正常工作,但如果不成功,用户仍然被保存,并且仍然发送确认电子邮件。
class RegistrationsController < Devise::RegistrationsController
def create
super
@user = resource
result = UserSignup.new(@user).sign_up(params[:stripeToken], params[:plan])
if result.successful?
return
else
flash[:error] = result.error_message
# TODO: OVERIDE SUPER METHOD SO THE CONFIRM EMAIL IS
# NOT SENT AND USER IS NOT SAVED / EXIT THE METHOD
end
end
我已经尝试了skip_confirmation!,这就绕过了确认的需要。resource.skip_confirmation_notification !也行不通。我还尝试重新定义resource.send_confirmation_instructions;零;结束;我的想法是在else块中完全退出创建方法。我如何退出创建方法或在else块中抑制"super",或者另一种方法更好?谢谢。
通过调用覆盖顶部的super
,整个注册过程将发生,注册您的用户,然后才执行您的代码。
你需要重写设计的registrations_controller。Rb通过复制和粘贴整个代码并像这样插入调用来创建操作代码:
class RegistrationsController < Devise::RegistrationsController
# POST /resource
def create
build_resource(sign_up_params)
# Here you call Stripe
result = UserSignup.new(@user).sign_up(params[:stripeToken], params[:plan])
if result.successful?
resource.save
else
flash[:error] = result.error_message
end
yield resource if block_given?
if resource.persisted?
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_flashing_format?
sign_up(resource_name, resource)
respond_with resource, location: after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
expire_data_after_sign_in!
respond_with resource, location: after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
set_minimum_password_length
respond_with resource
end
end
end
注意resource.save
只在result.successful?
时被调用