How do I remove the Devise route to sign up?

后端 未结 15 2329
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-07 07:36

I\'m using Devise in a Rails 3 app, but in this case, a user must be created by an existing user, who determines what permissions he/she will have.

Because of this, I

相关标签:
15条回答
  • 2020-12-07 08:10

    I had similar issue tried to remove devise_invitable paths for create and new :

    before:

     devise_for :users
    

    rake routes

    accept_user_invitation GET    /users/invitation/accept(.:format)           devise/invitations#edit
           user_invitation POST   /users/invitation(.:format)                  devise/invitations#create
       new_user_invitation GET    /users/invitation/new(.:format)              devise/invitations#new
                           PUT    /users/invitation(.:format)                  devise/invitations#update
    

    after

    devise_for :users , :skip => 'invitation'
    devise_scope :user do
      get "/users/invitation/accept", :to => "devise/invitations#edit",   :as => 'accept_user_invitation'
      put "/users/invitation",        :to => "devise/invitations#update", :as => nil
    end
    

    rake routes

    accept_user_invitation GET    /users/invitation/accept(.:format)                 devise/invitations#edit
                           PUT    /users/invitation(.:format)                        devise/invitations#update
    

    note 1 devise scope https://github.com/plataformatec/devise#configuring-routes

    note 2 I'm applying it on devise_invitable but it will work with any devise *able feature

    Important note: see that devise_scope is on user not users ? that's correct, watch out for this ! It can cause lot of pain giving you this problem:

    Started GET "/users/invitation/accept?invitation_token=xxxxxxx" for 127.0.0.1 
    Processing by Devise::InvitationsController#edit as HTML
      Parameters: {"invitation_token"=>"6Fy5CgFHtjWfjsCyr3hG"}
     [Devise] Could not find devise mapping for path "/users/invitation/accept?  invitation_token=6Fy5CgFHtjWfjsCyr3hG".
    This may happen for two reasons:
    
    1) You forgot to wrap your route inside the scope block. For example:
    
      devise_scope :user do
         match "/some/route" => "some_devise_controller"
      end
    
     2) You are testing a Devise controller bypassing the router.
       If so, you can explicitly tell Devise which mapping to use:
    
        @request.env["devise.mapping"] = Devise.mappings[:user]
    
    0 讨论(0)
  • 2020-12-07 08:10

    I've found this to work well without messing with routes or adding application controller methods. My approach is to override the devise method. Add this to app/controllers/devise/registrations_controller.rb I've omitted the other methods for brevity.

    class Devise::RegistrationsController < DeviseController
      ...
      # GET /resource/sign_up
      def new
        redirect_to root_path
      end
      ....
    end
    

    Also to remove illusion that this path is still reachable from other views you might also want to remove this code from app/views/devise/shared/_links.erb

    <%- if devise_mapping.registerable? && controller_name != 'registrations' %>
      <%= link_to "Sign up", new_registration_path(resource_name) %><br />
    <% end -%>
    
    0 讨论(0)
  • 2020-12-07 08:12

    This is an old question - but I recently had solve the same issue and came up with a solution which is far more elegant than:

    devise_for :users, :skip => [:registrations] 
    as :user do
      get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
      put 'users' => 'devise/registrations#update', :as => 'user_registration'
    end
    

    And it gives the default names for the named routes (like cancel_user_registration) without being excessively verbose.

    devise_for :users, skip: [:registrations]
    
    # Recreates the Devise registrations routes
    # They act on a singular user (the signed in user)
    # Add the actions you want in 'only:'
    resource :users,
        only: [:edit, :update, :destroy],
        controller: 'devise/registrations',
        as: :user_registration do
      get 'cancel'
    end
    

    rake routes output with the default devise modules:

                      Prefix Verb   URI Pattern                    Controller#Action
            new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
                user_session POST   /users/sign_in(.:format)       devise/sessions#create
        destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
               user_password POST   /users/password(.:format)      devise/passwords#create
           new_user_password GET    /users/password/new(.:format)  devise/passwords#new
          edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                             PATCH  /users/password(.:format)      devise/passwords#update
                             PUT    /users/password(.:format)      devise/passwords#update
    cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
      edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
           user_registration PATCH  /users(.:format)               devise/registrations#update
                             PUT    /users(.:format)               devise/registrations#update
                             DELETE /users(.:format)               devise/registrations#destroy
    
    0 讨论(0)
  • 2020-12-07 08:16

    I tried to do this as well, but a thread on the devise google group dissuaded me from searching for a really clean solution.

    I'll quote José Valim (the Devise maintainer) :

    There isn't a straight-forward option. You can either provide a patch or use :skip => :registerable and add only the routes you want.

    The original question was :

    Is there any good way to remove a specific route (the delete route) from Rails?

    0 讨论(0)
  • 2020-12-07 08:18

    By changing the routes there are a whole bunch of other problems that come with that. The easiest method I have found is to do the following.

    ApplicationController < ActionController::Base
      before_action :dont_allow_user_self_registration
    
      private
    
      def dont_allow_user_self_registration
        if ['devise/registrations','devise_invitable/registrations'].include?(params[:controller]) && ['new','create'].include?(params[:action])
          redirect_to root_path
        end
      end
    end
    
    0 讨论(0)
  • 2020-12-07 08:21

    You could modify the devise gem itself. First, run this command to find the installed location of using:

    gem which devise

    Let's suppose the path is: /usr/local/lib/ruby/gems/1.9.1/gems/devise-1.4.2/lib/devise

    Then go to

    /usr/local/lib/ruby/gems/1.9.1/gems/devise-1.4.2/lib/devise/lib/devise/rails and edit routes.rb in that directory. There is a method called def devise_registration(mapping, controllers) which you can modify to get rid of the new action. You can also completely remove the mappings for devise_registration

    0 讨论(0)
提交回复
热议问题