As a student of Microverse, I’ve reached the point where Rails’ framework was introduced to me. It appeared to me with the best omens on its hands. on Rails is a starting point for many start-ups because all of them got blinded by its beauty and simplicity. I have to admit that I felt intimidated for a while. Surely I can't say we are the best friends now but things started to make sense. Ruby on Rails is designed under the principles - Model View Controllers. Ruby MVC As things started to get more advanced, after 4 months of coding in Microverse, the time has come to create something real. But wait, how do they include user functionality in applications? Where are the credentials being kept and how can I make it happen without re-inventing the wheel. Devise is a flexible authentication solution for Rails Those types of questions kept spinning in my mind in the beginning. Fellow students advised me to calm down and spend time on documentation. Rails' world is surrounded by libraries called gems. There is a gem for anything you can think of. Without wasting more time let's see Devise magic in action. Let's create a Rails together. app rails new devise_gem rails g scaffold Post post:text rails db:migrate rails s Starting from the top, we a new rails app called initialize devise_gem. We use the scaffold command to create all of the necessary files for us without having to type all these on our own. Post is going to be our model. You can replace it with whatever you want. Text is the column type our model is in the database. Migrate the database for our application. Run the server. Check the browser and confirm that everything working well. You should see the welcome page of in the Rails localhost:3000 Application’s basic structure has been created for us. That’s great right? update routes => config/routes.rb - root restart server always modifying routes controllers Visit Gemfile root your application 'posts#index' the after files or files in the folder of Many gems-libraries are already there for you by default. We won't deal with these at the moment. Add in the general dependencies(this means anywhere outside of groups. Yes, you can put it between the comments. You can also leave your comment above in case you forget what this is. devise gem [EXTRA TIP]: Visit . It will be your friend from now on. rubygems.org ‘devise’ gem Back in the terminal and to get the devise dependencies in your project. bundle install On this step, we need to set up devise for our application. It simple but make sure you won't miss any step while doing that. (Did that a few times) Devise installation will ask you to follow some extra steps. Please, . Remember that we have already set our and even though it's not as devise tells us, don't get confused, because devise gives us an example. This mean there are only 3 steps left. rails g devise:install execute it one by one route file Lastly, after running all the commands, we have to do one more! Don't yell at me now with all your complaints. All of them are necessary for different reasons. rails g devise User . You have everything that you need in order to have your User functionality in your application. From now on, everything has to do with the way you want to use it. I will show you some basic things but feel free to test it yourself. That was all visit Inside the and in the let's add one line: app/controllers/posts_controller.rb class top before_action :authenticate_user!, : [:index, :show] except This tells to our application that users -meaning the ones that haven’t been registered in our database- can only visit our and page. un-authenticated index show By default, . If we want to have other fields as well - which we will do now - we need to do some things. devise gem has only email and password as fields Create RegistrationsController - (app/controllers/registrations_controller.rb) private params. ( ).permit( , , , , ) params. ( ).permit( , , , , , ) < Devise::RegistrationsController class RegistrationsController def sign_up_params require :user :name :username :email :password :password_confirmation end def account_update_params require :user :name :username :email :password :password_confirmation :current_password end Then, we have to update our routes with our controller ( ) new config/routes.rb users, controllers => { } devise_for : registrations: 'registrations' Next, let’s create a with the new we want to request from the user. Up to now, they and we want to include them in case you . migration file fields weren't in our database didn't get it yet g migration - CamelCase g migration - snake_case rails AddFieldsToUser rails add_fields_to_user Both of them do exactly the . Choose whatever suits better for you but be same job careful while typing. Everything matters on Rails. Check file cause we want to of code there. the method: migration include some lines Inside change add_column , , add_column , , add_index , , :users :name :string :users :username :string :users :username unique: true You may have noticed that we haven't done anything with our since we generated our scaffold model. Until now, we are still dealing with the user and configuration itself. I'm sure that you are thinking that all these were too much but surely it's worth our time because if we wanted to do that all these methods on our own, we would have spent . database Post setup devise double time Time for our migrations now: rails db :migrate From now on, we have new , and we need to our views located in the fields in our table update devise folder. <%= %> <%= %> <%= %> <%= %> app/views/devise/registrations/new.html.erb. < = > div class "field" f.label :name < /> br f.text_field , :name autofocus: true </ > div < = "> div class “field f.label :username < /> br f.text_field :username </ > div Create between associations controller && model app/model/user.rb model has_many :posts Post Model belongs_to :user rails g (generate) migration AddUserIdToPosts user_id:integer User # app/model/post.rb file should look like this: Migration add_column , , def change :posts :user_id :integer end After that being done, we have to configure our to work with . post_controller Devise = current_user.posts.build = current_user.posts.build(post_params) def new @post end def create @post end Devise provides us with lots of built-in methods like current_user, is_admin? etc. You can find all of the in the Devise Github repository. ( ) https://github.com/heartcombo/devise I know that I've said that again but keep in mind that it's always a good idea whenever you are making changes either to the controller or the routes to restart the server in your terminal to be sure that all of the changes are now loaded in the application. If you try now to a new Post as this is our root page you will arrive at the login page. Of course, we up to now. Let's do that if we want to enter our app and create some posts! create haven't created any user Basic things are introduced in this article. I was . There were cases where I felt like writing an to someone. If you haven't tried it out yet I you to spend some time with it. fascinated by the simplicity and how logical are things in Ruby thus in Ruby on Rails essay strongly recommend Last but not least, completely unrelated with the topic of this article but I thought it would be a good idea to add it as a conclusion. Remember that our minds love to play games with us. We want to learn everything at once. We are comparing ourselves with other people. We are feeling intimidating. Imposter Syndrome has huge impact in our life as developers. Rails may be outdated but if you are good at it, you will surely be of need. Choose your path and stay focused. Be healthy both physically and mentally! Github Profile : https://github.com/mariosknl Twitter : https://twitter.com/MariosKnl Portfolio: marioskanellopoulos.com Microverse: https://www.microverse.org/ Huge Credit to all my fellow students in Microverse and of course the school itself for giving as the chance to be involved in this great tech community. Thank you for spending time reading! Photo credits: Zane Lee