2015-02-07 55 views
2

我在我的RegistrationsController中創建了一個方法,該方法繼承自Devise :: Registrations控制器。它應該調用Stripe,如果客戶創建成功,它會保存用戶併發送一封確認電子郵件,該電子郵件由Devise中的「#create」處理。如果對Stripe的調用失敗,它應該設置一個閃存,而不是保存用戶或發送電子郵件,即禁止Devise'create'方法。如果對Stripe的調用成功,則此方法正常工作,但如果不成功,用戶仍會保存,並且仍會發送確認電子郵件。Ruby/Rails:壓制超類功能 - Stripe和Devise的集成

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塊中禁止「超級」,或者另一種方法會更好?謝謝。

回答

2

通過在覆蓋頂部調用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 

注意resource.save只稱爲如果result.successful?

+1

這工作,謝謝。我必須在控制器的私有方法中定義「設置最小密碼長度」,我從DeviseController中獲得它。我還將下一行'respond_with resource'更改爲redirect_to new_user_registration_path(plan:params [:plan])',以便註冊失敗或信用卡交易將用戶引導至相同的註冊頁面。 – user3291025 2015-02-08 01:52:27

+0

很高興知道!如果你認爲它會幫助別人,你可以編輯我的答案。 – dgilperez 2015-02-08 02:12:46