ruby-on-rails - ruby /Rails : suppress superclass functions - integration of Stripe and Devise

标签 ruby-on-rails ruby devise stripe-payments

我的 RegistrationsController 中有一个 create 方法,它继承自 Devise::Registrations Controller 。它应该调用 Stripe,如果客户创建成功,它会保存用户并发送确认电子邮件,该电子邮件由 Devise 中的“#create”处理。如果对 Stripe 的调用失败,则应该设置 flash 并且不保存用户或发送电子邮件,即抑制 Devise 的“create”方法。如果调用 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!,这只是绕过了确认的需要。资源.skip_confirmation_notification!也不起作用。我也尝试过重新定义resource.send_confirmation_instructions;零;结尾;我的想法是在 else block 中完全退出 create 方法。如何退出 create 方法或抑制 else block 中的“super”,或者其他方法会更好吗?谢谢。

最佳答案

通过在覆盖顶部调用 super ,整个注册过程将发生,注册您的用户,然后才执行您的代码。

您需要覆盖 Devise's registrations_controller.rb create action通过复制并粘贴整个代码并插入您的调用来编写代码,如下所示:

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

请注意,仅当 result.successful? 时才调用 resource.save

关于ruby-on-rails - ruby /Rails : suppress superclass functions - integration of Stripe and Devise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28388866/

相关文章:

ruby-on-rails - Devise中sign_in页面的路径助手的名称是什么?

ruby-on-rails - 设计身份验证 token

ruby-on-rails - 如何将电子邮件地址放入 Rails 的 url 中

ruby-on-rails - Rails 试图创建嵌套路由

ruby - $ GEM_HOME和$ GEM_PATH在旅客中设置错误

jquery - 验证检查带有单独提交按钮的动态文本框

ruby-on-rails - 如何 Hook 销毁属于另一个模型的模型?

ruby-on-rails - 每日、每周和每月页面查看计数器

javascript - 在主干中更改 url

html - 如何使用原始 html 显示内容