Devise routing: is there a way to remove a route from Rails.application.routes?

后端 未结 4 1148
温柔的废话
温柔的废话 2020-12-14 02:53

devise_for creates routes including a DELETE route, which we want to remove, and devise_for doesn\'t support an :except or :only

4条回答
  •  无人及你
    2020-12-14 03:23

    I found a simple solution with Devise 4.2.0 and Rails 5.0.1. I think this will work with Rails 4, and I'm uncertain about older versions of Devise.

    Create an initializer overriding the devise_* route helpers. Examples methods are devise_session, devise_password, devise_confirmation, devise_unlock, and devise_registration. Check out the source.

    Ensure the initializer is loaded after the Devise initializer by giving the filename a larger alphanumeric value.

    For example, Devise creates a :confirmation route with the :new, :create, and :show actions. I only want the :create action.

    # config/initializers/devise_harden.rb
    module ActionDispatch::Routing
      class Mapper
    
        # Override devise's confirmation route setup, as we want to limit it to :create
        def devise_confirmation(mapping, controllers)
          resource :confirmation, only: [:create],
                   path: mapping.path_names[:confirmation], controller: controllers[:confirmations]
        end
      end
    end
    

    Now POST /auth/confirmation is the only route setup for confirmation.

提交回复
热议问题