Building Custom Routes for Authentication Pages with Devise in Rails 5
Learn how to implement custom routes for Devise based authentication pages, including new methods that have changed in Rails 5.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Looking at our project management dashboard, you can see the next item is to customize the routes.

I'm going to open this up in an incognito window because we have implemented a log out button yet, if I tried to go to users sign in or sign out it would just tell me that I was already logged in and I didn't need to do that again.

I'm going to go to localhost:3000/users/sign_up, I don't really like this as a path. I don't think this is very professional looking route. Then if you click on log in, you can see that it's very similar.

This is usually one of the first things I change. I'm going to come back to sublime text and let's open our routes.rb

In our routes, in this line for devise_for :users it can actually take some custom options. We are going to pass in a path that is going to take an empty string followed by a comma. Then path_names which takes a hash, so curly braces and inside the hash we can give the names of the paths that we want to change.

  • I want to change our sign_in path to 'login'
  • I want to change our sign_out to 'logout'
  • I want change our sign_up to 'register'

devise_for :users, path: '', path_names: {sign_in: 'login', sign_out: 'logout', sign_up: 'register' }

Hit save and if I come back to this incognito window and hit refresh this gives me an error.

This means that no longer is users/sign_in a route. If we go to localhost:3000/login this works. If I click on Sign up it takes me right to register.

With just one line of code we now have this.

In the next guide, we're going to implement the log out functionality.

  • git status
  • git add .
  • git commit -m "Customized registration, login and logout routes."
  • git push origin authentication

I'm not going to close out of this one subtask for pivotal tracker yet because yes we did customize routes but we still have one piece of this that's left undone which is our logout button.