Let’s start learning Ruby on Rails

Tram Ho

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:

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.

If not, you can install with the following command:

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:

To check if the installation was successful, you can use the following command to check:

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:

After moving to the area for practice, we will start creating the first application. I will take the application name is hello_app .

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:

to see what the directory has.

app /The core of the application is here, including Models, Views, Controllers and Helpers
app / assetsApplication 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 / assetsLibrary 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 / assets3rd party resources such as CSS files, JS files, images
GemfileThe gem is used for the app
Gemfile.lockList of gems used to ensure that all copies of the application use the same gem version
config.ruA 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:

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:

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:

Chia sẻ bài viết ngay

Nguồn bài viết : Viblo