Continuing my Let’s Build with Ruby on Rails – Extending Devise series I take a look at Devise custom routing options within a Ruby on Rails application harnessing the Devise gem.
Download the Kickoff Tailwind Template I reference in this tutorial (optional):
https://github.com/justalever/kickoff_tailwind
In this guide, you’ll learn how to use my kickoff_tailwind
template (linked above) to install and create a new Ruby on Rails app. Among many other configurations, the template sets up Devise for quick use immediately. Out of the box some defaults come with devise but I add a couple of new fields (username, name) to each given user account.
Customizing routing is quite easy in the context of the devise gem. A basic install on a fresh ruby on rails app might look like the following:
Rails.application.routes.draw do
devise_for :users # default custom method for rendering all routing
root to: "home#index" # devise requires some sort of root option to redirect back to
end
Extending the default paths from say /users/sign_in
or /users/sign_up
might be something you want to customize based on your own apps logic and/or branding. Doing so it pretty straight forward. In the end I made the following changes.
Rails.application.routes.draw do
devise_for :users,
path: '', # optional namespace or empty string for no space
path_names: {
sign_in: 'login',
sign_out: 'logout',
password: 'secret',
confirmation: 'verification',
registration: 'register',
sign_up: 'signup'
}
root to: 'home#index'
end
Above you can see how the defaults are overwritten with string values which essentially just update the URL pathnames accordingly. You don’t need to modify view or controller names in your app which is great but also kind of confusing as most of the time those things match. I’ve worked with Devise long enough for this issue to not affect me a great deal but it might be something worth considering for your own projects.
The Series So Far
- Let’s Build: With Ruby on Rails – Extending Devise Series – Adding Custom Fields
- Let’s Build: With Ruby on Rails – Extending Devise Series – Confirmation Emails
Shameless plug time
I have a new course called Hello Rails. Hello Rails is a modern course designed to help you start using and understanding Ruby on Rails fast. If you’re a novice when it comes to Ruby or Ruby on Rails I invite you to check out the site. The course will be much like these builds but a super more in-depth version with more realistic goals and deliverables. Sign up to get notified today!
Follow @hello_rails and myself @justalever on Twitter.
The post Let’s Build with Ruby on Rails – Extending Devise – Custom Routing appeared first on Web-Crunch.
Top comments (0)