Override devise registrations controller

前端 未结 5 1732
渐次进展
渐次进展 2020-11-22 00:05

I have added a field to the sign-up form that is based on a different model, see How do I use nested attributes with the devise model for the gory details. This part is work

5条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 00:49

    A better and more organized way of overriding Devise controllers and views using namespaces:

    Create the following folders:

    app/controllers/my_devise
    app/views/my_devise
    

    Put all controllers that you want to override into app/controllers/my_devise and add MyDevise namespace to controller class names. Registrations example:

    # app/controllers/my_devise/registrations_controller.rb
    class MyDevise::RegistrationsController < Devise::RegistrationsController
    
      ...
    
      def create
        # add custom create logic here
      end
    
      ...    
    
    end 
    

    Change your routes accordingly:

    devise_for :users,
               :controllers  => {
                 :registrations => 'my_devise/registrations',
                 # ...
               }
    

    Copy all required views into app/views/my_devise from Devise gem folder or use rails generate devise:views, delete the views you are not overriding and rename devise folder to my_devise.

    This way you will have everything neatly organized in two folders.

提交回复
热议问题