Ruby on Rails is a framework based on Ruby programming language, it is mainly used for web development. what do you need to know about it?
This framework uses model view controller architecture, which makes easy many aspects when developing the web, however, as many people know or if you do not know it, this architecture focuses the views on the front end development, the Html, CSS, JS, bootstrap, and preprocessors like SASS and CoffeeScript. The controllers focus on the website’s logic, interacting with views and models, and models focus on the data to be stored on the database.
On rails, we can create controllers, models, and views with the command line or manually. The next two commands show how to create a model and a controller.
$rails generate model model-name $rails generate controller controller-name
When we create a controller, it automatically creates an empty view folder called the same way. It is possible to define or add methods in the same command as it is shown on the next command, where we create a controller called homes.
. which creates the methods contact and info in the controller’s class, at the same time it creates the routes, the views contact, and info in the folder app/views/homes/.
$rails generate controller Homes contact info
The second and third lines, on the picture above, are routes generated automatically, they are usually used on the browser, to tell the controller to do a specific task or to show us a specific view; there are some other ways to use them, but this article is not focused on routes.
There is one important command,
$rails generate scaffold name parameter
this command is really useful to create a restful controller, it means it has the methods index, new, edit, show, create, update, and destroy. At the same time, it creates the views, index, new, show, edit, the model, the migration with the parameters listed at the end of the command, and resources routes.
The example above generates the Posts controller, the Post model, the migration with the attributes title string type, and body text type.
Migration is a file that stores the tables information, parameters and associations to be migrated to the database. So it saves us time on creating and setting up a database with its tables. it does the dirty job for us. When the migration is ready and we want to migrate it, we just type
One of the first things you want to do is to see how the app is working on the browser, for this purpose you can use the command $rails server or $rails s, which launches the server by default on the port 3000 but if you want to use a different port you can add -p and the new port number,
$rails server -p 4000
When you want to test your app starting with the model, migration, and database, without taking into account neither controllers nor views. Rails console command will help you to test it on the console, in which you can create objects, save then on the database and retrieve them with associations or any test you want to do related to back-end.
A good command when you create a new rail app and you want to specify the type of databases you want to use it is the next one:
$ rails new . - -database=postgresql
$rails new . - -database=mysql
Which generates some lines on the file config/database.yml, setting up the chosen database type.
In general terms, I can say that ruby on rails is an excellent framework to work with, of course, you can find more frameworks based on different programming languages, like Laravel which is based on PHP; in my opinion, I have found easier to work with rails, but the logic is about the same, so when you master a framework like this one, it would be easy to learn a new one.
Previously published at https://medium.com/@javiercaliescali/what-you-need-to-know-about-ruby-on-rails-to-start-your-first-app-417c00f32d94