I was building a marketplace app where I required 2 types of users; normal users and business owners. This is a post on how I create 2 registrations with 1 user model using Devise.
The Devise documentation talks about creating multiple models for different users like having an admins model but I wanted to stick to 1 user model and use roles for defining users as I feel this is a more flexible solution. With the 2 users I needed 2 different registration pages as the UX needs to be different for business owners as they’ll require more of a ‘sales pitch’ as to the benefits for their business.
Devise setup and implementing normal users
The first step was to get Devise up and running for the normal users. This is quite easy and all is explained on the devise github page. In short generate the devise users model and setup the model with the required devise modules.
I wanted to make some changes to the normal users registration process so I also generated the devise controllers and adjusted the routes to point to this controller.
$ rails generate devise:controllers users
Implementing the business owners users
After setting up the normal users and getting that all working I could then create a secondary registration process for the business owners. I wanted:
A seperate registration page using a different layout to create more a sales pitch type page
Require extra user attributes for business owners e.g. full name, contact number
Assign the ‘business owners’ role to the user upon sign up
After sign up redirect to a page for setting up their store profile
I generated a new rails controller for the business owners registration and then proceed to write some controller tests using rspec.
After creating my tests I then created my routes.
What I have here is:
a get route for the business owners registration page
a post route when the user submits from the get route above
a get route for edit, this is if a normal user is signed in they can still register as a buisiness owner
then the last 2 routes are put and patch for the get edit route above
Then I implemented my business owners registration controller.
Firstly this controller is inheriting from Devise::RegistrationsController that allows me to call super in the methods to access the parents method of the same name. So as you can see the new and edit methods simply call super which prepares a devise resource (a user object).
The other 2 CRUD methods create and update call super but then have some extra logic for assigning the ‘business_owner’ role to the user.
Require more information for a business owner to register
I wanted to create a good UX so for the sign up I only wanted to collect the bare mimimum. For normal users I was just requiring the defaults of email and password but for business owners I needed at least their full name and contact number.
To enforce these extra attributes just for business owners I used a virtual attribute and rails model validations. Firstly I pass this virtual attribute from the controller by merging it in the Devise parameter sanitizer.
Then in the user model I check if this attribute is there and if so run the validations I want.
Run tests and volia! I now have 2 different registration flows for the user types with all users going into the 1 model and being easily identified based on roles.
I would love some feedback on this so comment below to go into the draw to win a “ferrari”!*
winner is drawn at random compeition ends when there are 100 different commenters the “ferrari” is a digital picture of a ferrari drawn by me in mspaint, which is awesome!