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.
First you should use the command:
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.
➜ ~ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin18]
If not, you can install with the following command:
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:
gem install rails
To check if the installation was successful, you can use the following command to check:
➜ ~ rails -v
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:
➜ ~ cd Work
After moving to the area for practice, we will start creating the first application. I will take the application name is hello_app .
➜ Work rails new hello_app
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:
➜ Work cd hello_app
➜ hello_app git:(master) ✗ open .
to see what the directory has.
|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 / 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|
After the application has been created, we use the rails server command to launch the application. The result will look like this:
➜ hello_app git:(master) ✗ rails s
=> Booting Puma
=> Rails 220.127.116.11 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
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.
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:
class ApplicationController < ActionController::Base
render html: "hello, world!"
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:
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: