2013-11-02 196 views
0

試驗Devise 3.0 for Rails 4並考慮集成到現有應用程序中以取代我自己的身份驗證系統。覆蓋設計控制器

我自己的users_controller在註冊期間與API(條紋)進行通信,我不確定如果我使用Devise如何包含此功能?

我想以某種方式覆蓋/擴展設計控制器?任何幫助將非常感激。

回答

1

您可以在控制器中定義動作並在其中調用super,其中將包含相應設計動作的功能。你也可以添加你自己的功能。

例如:考慮中的註冊控制器(Registrations Controller

在這裏,你自己創建登記控制器的作用,並在代碼註釋添加功能,你可以使用登記控制器的色器件的創建行動的代碼。

class RegistrationsController < Devise::RegistrationsController 
    . 
    . 
    . 

    def create 
    build_resource(sign_up_params) 

    if resource.save 
     if resource.active_for_authentication? 
     set_flash_message :notice, :signed_up if is_navigational_format? 
     sign_up(resource_name, resource) 
     # Here the user successfully signs up, so you can use your stripe API calls here. 
     respond_with resource, :location => after_sign_up_path_for(resource) 
     else 
     set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format? 
     expire_session_data_after_sign_in! 
     respond_with resource, :location => after_inactive_sign_up_path_for(resource) 
     end 
    else 
     clean_up_passwords resource 
     respond_with resource 
    end 

    end 

end 
+0

感謝 - 但我如何檢測是否用戶註冊成功了嗎? – tommyd456

+0

所以你問的是註冊控制器的創建動作。 –

+0

是的 - 在創建操作中,如果用戶成功創建,那麼我想與Stripe API進行通信 - 我還需要更新新創建的用戶的屬性! – tommyd456

0

對於未來的Google:它是可以調用super在繼承控制器與塊的動作,做任何你想要的,而不會覆蓋設計代碼:

class Users::RegistrationsController < Devise::RegistrationsController 
    def create 
    super do |user| 
     NotificationMailer.user_created(user).deliver 
    end 
    end 
end