My Favorite Music of 2020

Welcome to my third year-end list of music from the past year and beyond. This year more than ever, music was a very important part of my life. These songs were the soundtrack for everything from…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Using devise in your Ruby on Rails application

Authentication. You don’t always want your users to have faceless sessions that open your application without leaving any trace. Of course, there are some applications in which you don’t need accounts for your users, but in the vast majority of the cases, you will need to manage some users and passwords. If you are a beginner with ruby on rails you may already be thinking how to do this. Create a user model, add some fields to your user model, including an encrypted password field. Save your user’s session in some way so they can remain online even after switching application windows, save some cookies so they can remain online even after closing the browser. You know, those things are pretty repetitive if you create new applications often, and if something is repetitive, you can make an algorithm that does it for you. The special thing about the internet though is that most of what you think about creating was already created by someone else. In the case of authentication for Ruby on Rails, there’s a gem for that.

Devise is a very complete gem that does all the authentication work for you, or most of it if you are thinking about a very specific feature you would want to implement. When I first used it, I just wanted to make a basic login for my users and add some extra features to the registration process. It took me a couple of hours to read the documentation and add what I wanted, and I wish I could have found a more basic tutorial to guide myself with. I want to do my past self a favor and make a short tutorial on how to create a Rails application, add some extra fields to the user model, and modify the default views of the gem.

Let’s begin. Create a project first if you haven’t already.

And immediately after, add devise to the gemfile.

Then run “bundle install” from your terminal. Wait for everything to install and when it’s done, run “rails generate devise:install” on your terminal.

You can see we got some instructions with this one.

The first one is referring to the mailer settings. For a development environment, you need to specify your default URL. This won’t give you trouble if you are not going to send mail to your users, but let’s copy and paste the line on the development.rb file just in case we need to send mail to our users.

The third point asks us to ensure we have flash messages on our application.html.erb file. Why? Well, this will let the users know if they are doing something wrong. Default messages are already included on the devise gem so you don’t have to write them. Just copy and paste what devise shows you on the terminal wherever you want it to be visible.

The last point is, in my opinion, the most important one. It’s telling us to do generate the views of devise for customization. We will run the command now and modify the files later.

Great! We can now generate our model. You can call your devise model whatever you want, I will call it “User”. It’s a generic name understandable by anyone. Let’s run the command now. Type “rails generate devise User” on your terminal, you should get something similar to this:

And before we migrate our database, let’s go check the migration file.

Here, you can uncomment the fields you want to use, for example, if you want to be able to confirm your users sending them an email, you can uncomment the four lines below # Confirmable. The devise gem will do the rest (plus some configuration of your own you will have to investigate, as it’s not going to be covered in this tutorial).

This will generate a new migration file which will add some columns to the user table. Let’s go check the migration file.

Remember, I didn’t write any of this on the migration file, it was generated by rails. Everything seems to be good, so we can do the database migration. On your terminal, you can either do “rails db:migrate” or you can also write “rake db:migrate”. Either will work. After doing the migration, let’s go check the sign-up form on our app. We haven’t done anything at the moment so we will need to access it manually by typing the route in. First of all, run your server by running “rails server” on your terminal.

This is the signup form, yes. But it’s lacking the two fields we wanted to our user. Let’s add them, actually, let’s add these two fields to both the new and edit files. The edit file, by the way, is for old users who want to change their information, like their email or password.

I just immitated the stile set by the auto-generated sign-up form from devise. You can style it later with your own css. Let’s see how it looks.

You should do the same with the edit.html.erb file while you are at it. For now, let’s test our sign-up form. I haven’t added any more functionality to this app, but we should be able to add something to our database with this form. Let’s first run the console and check if we have something there.

You can see we get an empty array when we user the User.all command on the console. Let’s go back to the sign-up form and… sign-up.

After clicking on the Sign Up button, we are sent back to the root route. This is the intended behaviour. Let’s go check the console again and see what we get when we search for all of the users. We should only get one user and it sould be mister foo bar.

Let me go to the console and erase the first user from there. Now let’s test this again, after the changes, we should be able to sign-up and have all of our user fields inserted in the database.

I’m using the same credentials as last time. Again, I already erased the first mister foo bar from the database so it shouldn’t be a problem. Now let’s click Sign Up and check the database on the console.

We got it now.

Congratulations, you now know how to install devise in your applications, add a model with devise, add some fields to those models and customize devise’s views. You can now continue with the rest of your web application.

Thank you for reading!

Add a comment

Related posts:

Astellia Global Service Launches!

BORA developer Way2Bit will collaborate with Studio 8, a subsidiary of Barunson E&A., for global game service of the online MMORPG Astellia series. Way2Bit took the first step toward building its own…

6 Hard Truths That Will Make Us Richer

The last week of the year is always about summing up the results. At school, grades are given, at the institute they pass tests, employees write reports, managers analyze. What about personal totals…