Let's Build with Ruby on Rails - Extending Devise - Custom Routing
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):
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:
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
Extending the default paths from say
/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.
path: '', # optional namespace or empty string for no space
root to: 'home#index'
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 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. View the course!