Building Apps for Startups with Ademar Tutor

Building Apps for Startups with Ademar Tutor

Share this post

Building Apps for Startups with Ademar Tutor
Building Apps for Startups with Ademar Tutor
Building user authentication and profiles in Rails with Devise

Building user authentication and profiles in Rails with Devise

The most common feature in any application is user authentication and profile creation. This guide helps you build that in a few steps in Rails using the Devise gem.

Ademar Tutor's avatar
Ademar Tutor
Jan 03, 2024
1

Share this post

Building Apps for Startups with Ademar Tutor
Building Apps for Startups with Ademar Tutor
Building user authentication and profiles in Rails with Devise
Share

What is Devise Gem?

With over 25,000+ stars and 5,000+ forks in Github, Devise is still Ruby on Rails's most popular user authentication gem. Devise is based on Warden. Devise handles user registration, session management, password recovery, and other user-related functionalities. It's designed to cater to various authentication needs with minimal effort.

How do you install Devise Gem in Rails 7?

  1. Setup rails app

  2. Add devise on freshly created Rails app's Gemfile and then run bundle install on terminal.

  3. Run devise installation script on terminal.

  1. Update development.rb by adding this line:

  1. Run the devise generator to create a model and configure it with the default Devise modules. In this case, we are generating the User model. The command also configures your config/routes.rb file to point to the Devise controller.

  1. Run the migration in the terminal.

  1. Let's create a homepage so we can quickly check Devise's authentication features.

  1. Let's add a navbar to add buttons to sign up, sign in, and log out. We can do this by editing the application.html.erb

  1. Let's add the home page to the routes.rb

Now, you can test the Devise features like login, log out, and sign out.


Building a User Profile with Devise Gem

Now that we've already added authentication, we need to add a profile for that user next.

  1. Create a Profile Model
    First, you need to create a Profile model associated with the User model. You can generate this model using Rails generators. For instance, if you want to include attributes like bio, age, and location, you would do something like:

  1. Set Up Associations
    You should define the association with the Profile model in your User model (app/models/user.rb). Typically, this is a one-to-one association. You would add:

    In your Profile model (app/models/profile.rb), set the reverse association:

  1. Migrate the Database

    Run the database migrations to update your database schema with the new Profile table:

  1. Creating the user profile
    You can use callbacks in your User model to automatically create or build a profile when a new user is registered and set up associations.

    On user.rb:



Allow users to update their Profile

Now that profiles are automatically created after the user signs up, let's create a feature for the user to edit their own profile.

  1. Create the ProfilesController
    First, you need to create a ProfilesController. This can be done manually or via a Rails generator:


  2. Define Actions in the Controller
    In app/controllers/profiles_controller.rb, you can define actions to handle profile updating. Here's a basic setup:

  3. Setup Routes
    In your config/routes.rb, you need to set up routes for the profiles:

  1. Create Views
    You should create views for the edit action in app/views/profiles/. Here's how you edit.html.erb view:

  2. Update the link on the navbar
    Let's update the navbar to add an "Update Profile" link whenever there is a signed-in user.



Subscribe to Building Apps for Startups with Ademar Tutor

Building Apps for Startups with Ruby on Rails, Hotwire & Turbo.
1

Share this post

Building Apps for Startups with Ademar Tutor
Building Apps for Startups with Ademar Tutor
Building user authentication and profiles in Rails with Devise
Share

Discussion about this post

User's avatar
Understanding Big-O Notation with Ruby
If the whole Big-O notation thing baffled you in college, guess what? You're in great company! 😅 Many of us had our share of head-scratching moments.
Jan 8, 2024 â€¢ 
Ademar Tutor
1

Share this post

Building Apps for Startups with Ademar Tutor
Building Apps for Startups with Ademar Tutor
Understanding Big-O Notation with Ruby
Dynamic AI Prompts with PromptTemplates, Ruby, Langchain.rb and OpenAI
This tutorial talks about how to build a Ruby Chatbot with Langchain, PromptTemplate and OpenAI.
Jan 14, 2024 â€¢ 
Ademar Tutor
4

Share this post

Building Apps for Startups with Ademar Tutor
Building Apps for Startups with Ademar Tutor
Dynamic AI Prompts with PromptTemplates, Ruby, Langchain.rb and OpenAI
Observability with OpenTelemetry, Jaeger and Rails
Discussion and setup of OpenTelemetry and Jaeger with Ruby on Rails
Feb 19, 2024 â€¢ 
Ademar Tutor
2

Share this post

Building Apps for Startups with Ademar Tutor
Building Apps for Startups with Ademar Tutor
Observability with OpenTelemetry, Jaeger and Rails

Ready for more?

© 2025 Ademar Tutor
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share

Create your profile

User's avatar

Only paid subscribers can comment on this post

Already a paid subscriber? Sign in

Check your email

For your security, we need to re-authenticate you.

Click the link we sent to , or click here to sign in.