今天做案子時,需要在 Devise user create 前後做一些事情,但是又不想污染 User model,以往的方法就是寫一個 Users::RegistrationsController 去繼承 Devise::RegistrationsController,然後把整個 method copy 過來一次:
123456789101112131415161718192021222324
classUsers::RegistrationsController<Devise::RegistrationsControllerdefcreate# 在這裡加入 before hookbuild_resource(sign_up_params)ifresource.save# 在這裡加入 after hookifresource.active_for_authentication?set_flash_message:notice,:signed_upifis_navigational_format?sign_up(resource_name,resource)respond_withresource,:location=>after_sign_up_path_for(resource)elseset_flash_message:notice,:"signed_up_but_#{resource.inactive_message}"ifis_navigational_format?expire_session_data_after_sign_in!respond_withresource,:location=>after_inactive_sign_up_path_for(resource)endelseclean_up_passwordsresourcerespond_withresourceendendend