A Quick-Start With Rails
A Quick-Start With Rails
22 September 2020
Rails is a framework built on the top of the ruby language. Rails is a web application framework and not just any framework it is one of the most powerful frameworks present in the web application world. Not going into history, let’s start with rails.
To get started with rails you just need to know the basics of the Ruby language. Here is some source to learn ruby:
- Don’t Repeat Yourself: Rails follows the DRY principle. It says do not write the same code again and use the existing code. A single piece of code needs to be unique. This principle makes rails code less buggy and more useful.
- Convention over configuration: This principle is one of the reasons that makes rails the most powerful framework. Convention over configuration means you don’t need to configure the application every time you make changes in the application, just do changes according to the defined convention.
- Let’s make the first rails application: In rails, there are generators as the name suggest these generators generate things for you to make things easier for you as a developer and rails come with many of the generators, we will talk about all the letter, first, we are going to use one them to generate our first rails application.
To use this generator, open the terminal and go to the directory where you want to generate a project and type ‘rails new product’. This command will create a Product application with the product directory. Here ‘product’ is an application name and you can change it.
After creating a product application, go to the product folder with the command ‘cd product’.
Now look at the folder and you can see there are many auto-generated files that are generated by the rails generator. Let’s find out what are they:
- app/: This folder contains all the controllers, models, views, helpers, mailers, channels, jobs. Basically, all the important files that you will work with most.
- bin/: This folder contains scripts related to setups and this is one of the folders you are going to interact less or almost never.
- config/: This folder contains all the routes, database configuration files, etc.
- db/: This folder contains a database schema and database migration file.
- Gemfile and Gemfile.lock: These folders contain all the gems (libraries) that you are going to use in your application.
There are also other folders present but initially, these are the only folders you need to know. If you want to know more about the directory structure of rails visit the rails official site.
By running ‘rails new product’ use just generated your first rails web application. You were thinking, how is it possible? , now lets run another command ‘rails server’ to run the application on our local server. Now go to the browser and hit your localhost. It will look something like this:
Let’s move further and for the upcoming example, we are going to create a mysql2 gem in our project by adding just gem ‘mysql2’ in our Gemfile and running bundle install to install the gem for our project.
Let’s know more about the generator by running simple command ‘rails generate -h’ it will look like something like this:
As you can see rails provide many generators like a model generator that can generate a model for the application, controller generator that can generate a controller for the application, etc.
Getting along with rails first we need to understand MVC architecture that rails strictly follows.
MVC stands for model-view-controller.
- Model: Models are the classes that interact with the database and contain all the business logic.
- Controller: Controller is the one that helps model and view to communicate with each other. The controller receives a request from the browser and responds after checking
Now we are going to use the scaffold generator which is the most powerful generator that rails provide. Let’s run the command and see the scaffold generator ‘rails generate scaffold Product’.
Scaffold generates a model, controller, view, helper, JBuilder, and assets for the product model all in a single command. It also generates a migration file that makes changes in our database. Scaffold generates routes for us so we don’t have to define the routes manually.
Scaffold generates all the code for basic CRUD (create, read, update, and delete) operations.
To perform all the operations first you have to create the database with running ‘rails db:create’ or ‘rake db:create’. To run the migrations and create the products table run ‘rails db:migrate’ or ‘rake db:migrate’
If you want to see what you have just done, run your local server by running just ‘rails server’ just hit your localhost:3000 with ‘products’ routes i.e localhost:30000/products. It will look something like this:
You can create a new product and after creating you can read, update and delete it.
How all this works?
- When a request comes from the browser, routes defined in the routes file will look for the requested route and respond to it. (For the product resources keyword generates all the valid routes if you want to see them run ‘rake routes’).
- If a request is valid then it will go for action (special method) present in a controller. The controller is also searched by name of the routes. (In our case when we hit a product route it will search for a product controller).
- If the action is found then it will perform operations present in that action and these operations are performed with the help of model and database (In our project when we hit products route it will look for index action and perform operations present in index action).
- When you look into the index action @products instance variable is present and it will be available for index view file according to the index action name.
- After performing the operation on the database it will render us to the corresponding view of the action.
If you want to read more about rails visit rails official site.