Introducing Rails
Ruby on Rails (or commonly referred to as Rails) is a framework for web development using Ruby language. Ever since its launch in 2004, Ruby on Rails has become one of the most popular and powerful development tools for web applications. Rails is used by companies like Airbnb, Basecamp, Disney, GitHub, Hulu, Kickstarter, Shopify, Twitter and Yellow Pages.
What makes Rails so much used by large companies? First, Rails is 100% open source, and with the MIT License , it’s free to use Rails. Rails is considered a lightweight design, by fully exploiting the Ruby language, Rails has been created specifically for writing web applications. As a result, common tasks for web programming such as creating HTML, Modeling and URL routing become easy when using Rails. When reading the code in the application is also very easy because it is very brief.
Rails also adapts quickly to new web development technologies. For example, Rails is one of the frameworks that implement the REST architecture for its web application.
Finally, Rails thanks to an enthusiastic support community with thousands of open source contributions, knowledge-sharing exchanges around the world, a large amount of Gem (I understand that the code packages have been written already ) have been created. Rails is getting better and better.
Install Rails
First you should use the command:
1 2 | ruby -v |
on the terminal to check if you have installed Ruby. If the results are similar to this, then you already have Ruby installed on your computer.
1 2 3 | ➜ ~ ruby -v ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin18] |
If not, you can install with the following command:
1 2 | brew install ruby |
The instructions above are for MacOS, if you use another operating system, you can refer here .
Once you have installed Ruby on your computer and then installed Rails, we will use the gem command provided by the RubyGems management package:
1 2 | gem install rails |
To check if the installation was successful, you can use the following command to check:
1 2 3 | ➜ ~ rails -v Rails 6.0.2.2 |
Create the application with Rails
Following the long tradition in programming, after setting the environment, we will create a Hello World application. And this time we will create that application using Rails. To create a Rails application, Rails has supported us a very simple command that is new rails . First, before creating the application, I recommend that you move to the directory dedicated to the practical applications. For example, in my computer:
1 2 3 | ➜ ~ cd Work ➜ Work |
After moving to the area for practice, we will start creating the first application. I will take the application name is hello_app .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | ➜ Work rails new hello_app create create README.md create Rakefile create .ruby-version create config.ru create .gitignore create Gemfile . . . create storage create storage/.keep create tmp/storage create tmp/storage/.keep remove config/initializers/cors.rb remove config/initializers/new_framework_defaults_6_0.rb run bundle install . . . |
After running rails new we will get the above result as if it was successful. Let’s take a look at the results, you run the command:
1 2 3 | ➜ Work cd hello_app ➜ hello_app git:(master) ✗ open . |
to see what the directory has.
Folder | Purpose |
---|---|
app / | The core of the application is here, including Models, Views, Controllers and Helpers |
app / assets | Application resources such as CSS files, JS files, images |
bin / | The binary file |
config / | Configuration of the application |
db / | Database files |
doc / | Documentation for the application |
lib / | Library |
lib / assets | Library resources such as CSS files, JS files, images |
log / | The log files of the application |
public / | Data is publicly accessible (for example, via a web browser), such as error pages |
kiểm TRA/ | Test of the application |
tmp / | Temporary files |
vendor / | 3rd party code |
vendor / assets | 3rd party resources such as CSS files, JS files, images |
Gemfile | The gem is used for the app |
Gemfile.lock | List of gems used to ensure that all copies of the application use the same gem version |
config.ru | A configuration file for the Middleware Rack software |
Rails server
After the application has been created, we use the rails server command to launch the application. The result will look like this:
1 2 3 4 5 6 7 8 9 10 11 | ➜ hello_app git:(master) ✗ rails s => Booting Puma => Rails 6.0.2.2 application starting in development => Run `rails server --help` for more startup options Puma starting in single mode... * Version 3.12.4 (ruby 2.5.1-p57), codename: Llamas in Pajamas * Min threads: 5, max threads: 5 * Environment: development * Listening on tcp://localhost:3000 Use Ctrl-C to stop |
Model-View-Controller (MVC)
As soon as we start learning about Rails, we also need to know about the MVC model to have an overview of the Rails application in action. As the illustration:
You can see that the Rails application structure has an app / directory, including subfolders called models , views , and controllers . This is a suggestion that Rails always follows the model-view-controller (MVC) model, which separates the application data and the code used to display it.
When interacting with the Rails application, the browser will send a request, the web server will receive and transfer to the Rails application’s controller , it will be responsible for what to do next. In some cases, the controller will immediately render the display interface through the view , the view contains the template to convert to HTML, the controller takes these HTML and sends it back to the browser. In more common cases, the controller can also get information from the model , which is the object Ruby is responsible for communicating with the Database. After calling the model , the controller will put this information into HTML through the view and return it to the browser as HTML.
Hello World
After starting the application and having an overview of how Rails works, we continue with the initial goal set. Go to app / controllers / application_controller.rb and add the following code:
1 2 3 4 5 6 | class ApplicationController < ActionController::Base def hello render html: "hello, world!" end end |
So when calling hello inside the ApplicationController controller , it will return 1 html containing the text “hello, world!”. In order to navigate right into the site, we will enter this action, we need to configure a little more in the config / routes.rb file:
1 2 3 4 | Rails.application.routes.draw do root 'application#hello' end |
And here is the finished one, we use Ctrl + C to stop the server and reboot using the rails server . And this is the result:
I would like to end the article here. Thank you for taking the time to read this article. Reference links: