DEV Community

Cover image for Extending the default user devise
Artur
Artur

Posted on

Extending the default user devise

Blogging day two, as a seasoned blogger I had a full head of ideas for this post (which is about two, both of them bad). Welcome to my 3 faithful followers, welcome to all who have not yet joined this elite group. Today I might even be able to show some code. I warn you that after seeing this your therapist will need therapy himself.
And this is just the beginning.

Now seriously, first a few words about what I write: an app for personal trainers and their trainees. Arranging workouts, diets, etc. To make things clear, this is a commercial project for which I will be paid and the client has agreed to make the repository public, which you can find here: https://github.com/kubacky/FitHustle

For today's workshop, we will take an extension of the default rails devise with the user's name and roles. To use devises at all, we need to add a gem with them. This is not very complicated and is limited to adding one line in our gemfile

gem 'devise'

Then we need to run

bundle install
and

rails generate devise:install

in the console. It would also be useful to have devise views in our application, so:

bundle exec rails g devise:views

and finally:

rails generate devise User

We still need to migrate the changes generated by the above command

rake db:migrate

We got it!
To extend the default functionality we need to extend the available devise controllers, so in the console we quickly type:

rails generate devise:controllers users

and voilà. Almost the first coaches can arrange diets for their players. What's left is to extend the default user table with first name, last name and roles. The console is of course on standby at all times, so here we go:

rails g migration add_name_and_role_to_users name:string surname:string role:string

This will create a file in the folder: db/migration with a strange name like: 20210420195443_add_name_and role_to_users.rb

We can now preview this file, which should look like this:

class AddNameAndRoleToUsers < ActiveRecord::Migration[6.1]
  def change
    add_column :users, :name, :string
    add_column :users, :surname, :string
    add_column :users, :role, :string
  end
end
Enter fullscreen mode Exit fullscreen mode

And again in the console:

rake db:migrate

In the next post we will add the tables for trainers and trainees in the database ;)

Cover photo: Luna think she’s an ostrich

Oldest comments (0)