1

I'm trying to add a simple custom action cancelaccount to a user model generated by devise, but having a hard time getting the routing right.

Here is what's in route.rb

devise_for :users, controllers: { registrations: "userregistrations", sessions: "sessions" } do        
  get '/users/:id/cancelaccount', to: 'userregistrations#cancelaccount', as: 'cancelaccount'   
end

Here is the controller

class UserregistrationsController < Devise::RegistrationsController      
  def create
  ...
  end

  def cancelaccount
      authenticate_user!
      Rails.logger.debug {"&& cancel"}
      @user = User.find(params[:id])
      unless (@user == current_user)
          redirect_to :back, :alert => "Access denied."
      end
  end
end

Here is the link_to line in the HTML:

<li><%= link_to "Cancel Account", {controller: "userregistrations", action: "cancelaccount", id: current_user.id} %></li>

This is the error I get:

No route matches {:action=>"cancelaccount", :controller=>"userregistrations", :id=>12}

If I change the link_to link to:

<li><%= link_to "Cancel Account", cancelaccount_path(@user) %></li>

The error is:

undefined method `cancelaccount_path' for #<#:0x00000006a5d4c0>

Any help is highly appreciated.

Here is the result of "rake routes":

          new_user_session GET      /users/sign_in(.:format)               sessions#new
              user_session POST     /users/sign_in(.:format)               sessions#create
      destroy_user_session DELETE   /users/sign_out(.:format)              sessions#destroy
   user_omniauth_authorize GET|POST /users/auth/:provider(.:format)        devise/omniauth_callbacks#passthru {:provider=>/(?!)/}
    user_omniauth_callback GET|POST /users/auth/:action/callback(.:format) devise/omniauth_callbacks#:action
             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)                userregistrations#cancel
         user_registration POST     /users(.:format)                       userregistrations#create
     new_user_registration GET      /users/sign_up(.:format)               userregistrations#new
    edit_user_registration GET      /users/edit(.:format)                  userregistrations#edit
                           PATCH    /users(.:format)                       userregistrations#update
                           PUT      /users(.:format)                       userregistrations#update
                           DELETE   /users(.:format)                       userregistrations#destroy
                      root GET      /                                      static_pages#home
                     users GET      /users(.:format)                       users#index
                           POST     /users(.:format)                       users#create
                  new_user GET      /users/new(.:format)                   users#new
                 edit_user GET      /users/:id/edit(.:format)              users#edit
                      user GET      /users/:id(.:format)                   users#show
                           PATCH    /users/:id(.:format)                   users#update
                           PUT      /users/:id(.:format)                   users#update
                           DELETE   /users/:id(.:format)                   users#destroy
           userpreferences GET      /userpreferences(.:format)             userpreferences#index
                           POST     /userpreferences(.:format)             userpreferences#create
        new_userpreference GET      /userpreferences/new(.:format)         userpreferences#new
       edit_userpreference GET      /userpreferences/:id/edit(.:format)    userpreferences#edit
            userpreference GET      /userpreferences/:id(.:format)         userpreferences#show
                           PATCH    /userpreferences/:id(.:format)         userpreferences#update
                           PUT      /userpreferences/:id(.:format)         userpreferences#update
                           DELETE   /userpreferences/:id(.:format)         userpreferences#destroy
                    signup GET      /signup(.:format)                      devise/registrations#new
              edit_profile GET      /edit_profile(.:format)                devise/registrations#edit
           change_password GET      /change_password(.:format)             devise/passwords#edit
                   sign_in GET      /sign_in(.:format)                     devise/sessions#new
                  sign_out GET      /sign_out(.:format)                    devise/sessions#destroy
              confirmation GET      /confirmation(.:format)                devise/confirmations#new
            unlock_account GET      /unlock_account(.:format)              devise/unlocks#new
                           GET      /signup(.:format)                      trainer/registrations#new
                      edit GET      /edit(.:format)                        trainer/registrations#edit
                           GET      /sign_in(.:format)                     devise/sessions#new
                           GET      /sign_out(.:format)                    devise/sessions#destroy
                           GET      /confirmation(.:format)                devise/confirmations#new
                           GET      /unlock_account(.:format)              devise/unlocks#new
2
  • can you post your rake routes output here? Dec 21, 2014 at 20:20
  • Thanks Rustam, I just added the results
    – Dan
    Dec 21, 2014 at 20:50

1 Answer 1

0

I am not sure what is wrong with your devise_for block, but you can easily add this route outside of it:

devise_for :users, controllers: { registrations: "userregistrations", sessions: "sessions" }
match '/users/:id/cancelaccount' => 'registrations#cancelaccount', via: 'get', as: 'cancelaccount'

Which will add the route you want:

cancelaccount GET    /users/:id/cancelaccount(.:format)                                       registrations#cancelaccount
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.