Ruby-on-rails – Devise changing default routes not working

deviseruby-on-rails

when i follow the Devise Wiki at https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes , my default route names do not change at all, here is my code :

MyApp::Application.routes.draw do
  root :to => "profile#index"     

  devise_for :users
  namespace :user do
    root :to => "profile#index"
  end

  as :user do
  get "/login" => "devise/sessions#new"
  get "/signup" => "devise/registrations#new"
  end

The two route changes didnt work so i stopped.

How do i change it so my routes are:

/users/sign_in = /login

/users/sign_up = /signup

/users/sign_out = /signout

/users/edit = /edit

I am using Devise 1.3.4 and Rails 3.0.7.

Thank you in advance!

Best Answer

OK i figured it out, ill just type this out to help other newbies!

The routes themselves change but not the navigation links unless coded that way. try http://localhost:3000/login and http://localhost:3000/users/sign_up, they are the same thing but just have to be typed in manually.

My new code looks like this ( navigation links & route configurations together):

routes.rb:

MyApp::Application.routes.draw do

  devise_for :users do
    root :to => "devise/registrations#new"
    get "/" => "devise/registrations#new"
    post '/' => 'registrations#new', :as => :new_user_registration 
    match '/', :to => 'devise/registrations#new'    
    get "/edit" => "devise/registrations#edit"
    match '/edit', :to => 'devise/registrations#edit'   
    get "/login" => "devise/sessions#new"
    match '/login', :to => 'devise/sessions#new'
    get "/logout" => "devise/sessions#destroy"
    match '/logout', :to => 'devise/sessions#destroy'   
  end



  namespace :user do
    root :to => "profile#index"
  end

the views/devise/menu/_login_items.html.erb :

<% if user_signed_in? %>
  <li>
  <%= link_to('Logout', logout_path) %>        
  </li>
<% else %>
  <li>
  <%= link_to('Login', login_path)  %>  
  </li>
<% end %>

the views/devise/menu/_registration_items.html.erb

<% if user_signed_in? %>
  <li>
  <%= link_to('Edit account', edit_path) %>
  </li>
<% else %>
  <li>
  <%= link_to('Sign up', root_path)  %>
  </li>
<% end %>

I certainly hope this helps people who were lost like me and just got into both devise and RoR. it will give you a good understanding on how to get the routes to look the way you want it to be in a simple manner, yet still work. Good luck!