2016-12-04 80 views
1

我開始玩Devise,但認爲我缺乏自由,當我按照Michael Hartl教程直接使用Rails進行身份驗證時。然後,我爲我的Devise模型'客戶'生成了Devise控制器。所以,我可以玩了一下它:生成設計控制器是空的

rails generate devise:controllers clients 

雖然結果是空的控制器:

class Clients::RegistrationsController < Devise::RegistrationsController 
# before_action :configure_sign_up_params, only: [:create] 
# before_action :configure_account_update_params, only: [:update] 

    # GET /resource/sign_up 
    # def new 
    # super 
    # end 

    # POST /resource 
    # def create 
    # super 
    # end 

    # GET /resource/edit 
    # def edit 
    # super 
    # end 

    # PUT /resource 
    # def update 
    # super 
    # end 

    # DELETE /resource 
    # def destroy 
    # super 
    # end 

    # GET /resource/cancel 
    # Forces the session data which is usually expired after sign 
    # in to be expired now. This is useful if the user wants to 
    # cancel oauth signing in/up in the middle of the process, 
    # removing all OAuth session data. 
    # def cancel 
    # super 
    # end 

    # protected 

    # If you have extra params to permit, append them to the sanitizer. 
    # def configure_sign_up_params 
    # devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute]) 
    # end 

    # If you have extra params to permit, append them to the sanitizer. 
    # def configure_account_update_params 
    # devise_parameter_sanitizer.permit(:account_update, keys: [:attribute]) 
    # end 

    # The path used after sign up. 
    # def after_sign_up_path_for(resource) 
    # super(resource) 
    # end 

    # The path used after sign up for inactive accounts. 
    # def after_inactive_sign_up_path_for(resource) 
    # super(resource) 
    # end 

end 

所以我想真正的設計控制器不能在所有訪問,我們剛剛得到的控制器從繼承真正的隱藏Devise控制器?

+0

這個問題可能可以幫助,http://stackoverflow.com/questions/3546289/override-devise-registrations-controller –

+0

是的,我讀了這個線程。雖然我認爲生成Devise模型控制器會帶來實際的設備控制器。但它只是創建空的繼承控制器。總的來說,我發現它處理Devise事件相當困難,儘管它簡化了很多事情,比如電子郵件確認,... – Maxence

+0

是的,我也是這麼想的。現在我喜歡它。快樂的編碼。 –

回答

1

生成的控制器旨在通過從操作中刪除super來覆蓋默認行爲。無論如何,您可以複製並粘貼實際的代碼並隨時隨地播放。它在Github上可用。

您可以通過以下鏈接檢查整個源代碼,https://github.com/plataformatec/devise

Devise::RegistrationsController實際的代碼,

class Devise::RegistrationsController < DeviseController 
    prepend_before_action :require_no_authentication, only: [:new, :create, :cancel] 
    prepend_before_action :authenticate_scope!, only: [:edit, :update, :destroy] 
    prepend_before_action :set_minimum_password_length, only: [:new, :edit] 

    # GET /resource/sign_up 
    def new 
    build_resource({}) 
    yield resource if block_given? 
    respond_with resource 
    end 

    # POST /resource 
    def create 
    build_resource(sign_up_params) 

    resource.save 
    yield resource if block_given? 
    if resource.persisted? 
     if resource.active_for_authentication? 
     set_flash_message! :notice, :signed_up 
     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}" 
     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 

    # GET /resource/edit 
    def edit 
    render :edit 
    end 

    # PUT /resource 
    # We need to use a copy of the resource because we don't want to change 
    # the current user in place. 
    def update 
    self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key) 
    prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email) 

    resource_updated = update_resource(resource, account_update_params) 
    yield resource if block_given? 
    if resource_updated 
     if is_flashing_format? 
     flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ? 
      :update_needs_confirmation : :updated 
     set_flash_message :notice, flash_key 
     end 
     bypass_sign_in resource, scope: resource_name 
     respond_with resource, location: after_update_path_for(resource) 
    else 
     clean_up_passwords resource 
     set_minimum_password_length 
     respond_with resource 
    end 
    end 

    # DELETE /resource 
    def destroy 
    resource.destroy 
    Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name) 
    set_flash_message! :notice, :destroyed 
    yield resource if block_given? 
    respond_with_navigational(resource){ redirect_to after_sign_out_path_for(resource_name) } 
    end 

    # GET /resource/cancel 
    # Forces the session data which is usually expired after sign 
    # in to be expired now. This is useful if the user wants to 
    # cancel oauth signing in/up in the middle of the process, 
    # removing all OAuth session data. 
    def cancel 
    expire_data_after_sign_in! 
    redirect_to new_registration_path(resource_name) 
    end 

    protected 

    def update_needs_confirmation?(resource, previous) 
    resource.respond_to?(:pending_reconfirmation?) && 
     resource.pending_reconfirmation? && 
     previous != resource.unconfirmed_email 
    end 

    # By default we want to require a password checks on update. 
    # You can overwrite this method in your own RegistrationsController. 
    def update_resource(resource, params) 
    resource.update_with_password(params) 
    end 

    # Build a devise resource passing in the session. Useful to move 
    # temporary session data to the newly created user. 
    def build_resource(hash=nil) 
    self.resource = resource_class.new_with_session(hash || {}, session) 
    end 

    # Signs in a user on sign up. You can overwrite this method in your own 
    # RegistrationsController. 
    def sign_up(resource_name, resource) 
    sign_in(resource_name, resource) 
    end 

    # The path used after sign up. You need to overwrite this method 
    # in your own RegistrationsController. 
    def after_sign_up_path_for(resource) 
    after_sign_in_path_for(resource) 
    end 

    # The path used after sign up for inactive accounts. You need to overwrite 
    # this method in your own RegistrationsController. 
    def after_inactive_sign_up_path_for(resource) 
    scope = Devise::Mapping.find_scope!(resource) 
    router_name = Devise.mappings[scope].router_name 
    context = router_name ? send(router_name) : self 
    context.respond_to?(:root_path) ? context.root_path : "/" 
    end 

    # The default url to be used after updating a resource. You need to overwrite 
    # this method in your own RegistrationsController. 
    def after_update_path_for(resource) 
    signed_in_root_path(resource) 
    end 

    # Authenticates the current scope and gets the current resource from the session. 
    def authenticate_scope! 
    send(:"authenticate_#{resource_name}!", force: true) 
    self.resource = send(:"current_#{resource_name}") 
    end 

    def sign_up_params 
    devise_parameter_sanitizer.sanitize(:sign_up) 
    end 

    def account_update_params 
    devise_parameter_sanitizer.sanitize(:account_update) 
    end 

    def translation_scope 
    'devise.registrations' 
    end 
end 

你可以得到這裏,https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb

希望幫助!

+1

謝謝。我猜到了這一切,但我仍然不明白爲什麼不在控制器中顯示完整的代碼。對於像我這樣的新手來說更好,這樣我們可以直接調整控制器:添加參數等等......但是這樣我需要練習我的導軌,這也是很酷的。 :p – Maxence