Learn Ruby on Rails: Learn Ruby on Rails Series, Part 4

Daftar Isi

Introduction to the Rails framework

Rails is a web application framework created using the Ruby programming language. Rails was created to make the process of creating web applications easier and was created based on the needs of each developer. Rails allows developers to write shorter program code than other languages ​​or frameworks. According to developers who have tried Rails, Rails makes web application development more fun.

Rails has two philosophies that guide its creation

  1. Designed to make configuration easier for programmers because Ruby on Rails uses the principle of “Convention Over Configuration”. With this principle, programmers do not have to worry about configuring applications such as connecting to a database, as there is a mechanism provided and it is quite simple as one only needs to fill in the name of the type of database server used (mysql/oracle/sql server/postgresql/etc.), set the database name, username, password and host.
  2. Use the “DRY (Don’t Repeat Yourself)” principle, the essence of which is efficiency in code management.

Rails framework architecture

Rails has the concept of MVC (Model View Controller). What is a template? What is sight? and what is a controller?. Let’s answer them one by one.

  1. Model
    The models are ruby core application on rails whose function is to maintain an application. All logic And business rules in the application, this must be written in the template. For example, to validate a username so that it does not exceed 30 characters. Later, the data that will be entered into the database will be automatically validated and cannot be missed.
  2. See
    The view is the part whose function is to create user interface. For example HTML, XML, Ajax, etc. Show functions as the front end of an application.
  3. Controller
    The controller is the part whose function is receive “events” from outside (user input) and in service for connect model to view. For example, to retrieve data from user “A”. Take the data from the model, prepare it in the controller, and then the view takes care of displaying it on the web page.

MVC Flow on Rails, happens like this

In the image there is a Rails router board. This Rails router works to recognize and accept requests from URLs. Then throw it to the controller or directly to the application itself. To configure the rails router is located in the folder config/routes.rb

Rails has important components that are mutually durable. These components consist of:

  • Action Messaging
    This Action Mailer allows you to send emails from the web application you have created.
  • Action Pack
    Action Pack is a combination of the actions below.
    • Action controller
      This action controller is a C from the MVC concept in Rails. This controller usually works as an intermediary between the model and the view. It is also responsible for receiving requests from URLs, then doingto recover or save data and then use views to display the output in HTML format.
    • Stock allocator
      This action dispatcher works as an intermediate route controller request THE controller
    • Actions view
      This action is used by the action controller to convert the response produced by the controller
  • Active model
    The active template works to allow the Action Pack Wizard to interact with plain Ruby objects. This action also allows you to create a custom ORM (Object Relational Mapping). This action takes the form of libraries which contain the classes used by Active Record.
  • Active recording
    Active Record is the M of the MVC concept in Rails. It serves as a model in which it is part of the system responsible for data and business logic. This business data includes data about people, locations, etc.
  • Active resource
    Active resources are library which aims to provide an interface between Active Record to control resource Or object.
  • Active support
    Active Support is a component of Rails responsible for providing extensions, utilities, etc. Ruby.
  • The railways

Create a new Rails project

Rails provides a script call generator intended to facilitate the development process. In this tutorial, the author uses ruby version 3.0.0 And Rails version 6.1.3. To use the builder to create a new Rails project, run the following command


By the way, go to the blog folder


Structure of a Rails project

The folder that we often open and modify its contents when creating applications in Rails Framework is as follows.

  1. application/
    A place to store controllers, models, views, helpers, mailers, channels, tasks and assets.
  2. configuration/
    A place to store routes, databases, etc.
  3. db/
    Where to store database schema and migration
  4. Gemfile and Gemfile.lock
    This file allows the user to specify which gems are needed for the application.
To see the detailed functions of each folder, see here.

Running rails project

After creating a new project, try running the project by running the following command.

then try running it by typing the URL into a web browser. If the project is executed successfully, a page will appear as follows.

To stop the web server, do CTRL + C in the terminal or command prompt.

Creating controllers and views

To create a Rails project, you need at least one controller and one view. To create controllers and views we can use the builder provided by Rails.


At the command prompt or terminal, run the following command.

$ rails generate controller Welcome index

This means that the Rails builder creates a controller with a name welcome_controller.rb and we find there method index. Sin the autogenerator view create a new folder in the views section with name at home and inside there is a file index.html.erb.

Isi welcome_controller.rb
Isi file index.html.erb in app/views/welcome folder

If you want to create controllers and views manually, you can do so:

  1. Create a controller, it must have an extension .rb
  2. In the controller you must create at least one method
  3. To create a view, first create a folderand the name is adjusted to the controller name.
  4. Just in this folder create a view page with a name that matches the method on the controller and the extension should be .html.erb

Say “Hello!” » towards the rails

After creating the controller and view, you need to modify the contents of the index.html.erb file as follows.

Save the method in the route located at config/routes.rbas follows.

This script means we set the url /welcome/index which, if one accesses the route, will throw it to method index on welcome_controller.rb.

Important:
If we create a controller and visualize through a builder, the method will be automatically saved in the route.

Restart the Rails project by running the command rails m. In browsers
enter the url /welcome/index in a web browser.

Fathara Annisa Azka | C-aio Indonesia

PakarPBN

A Private Blog Network (PBN) is a collection of websites that are controlled by a single individual or organization and used primarily to build backlinks to a “money site” in order to influence its ranking in search engines such as Google. The core idea behind a PBN is based on the importance of backlinks in Google’s ranking algorithm. Since Google views backlinks as signals of authority and trust, some website owners attempt to artificially create these signals through a controlled network of sites.

In a typical PBN setup, the owner acquires expired or aged domains that already have existing authority, backlinks, and history. These domains are rebuilt with new content and hosted separately, often using different IP addresses, hosting providers, themes, and ownership details to make them appear unrelated. Within the content published on these sites, links are strategically placed that point to the main website the owner wants to rank higher. By doing this, the owner attempts to pass link equity (also known as “link juice”) from the PBN sites to the target website.

The purpose of a PBN is to give the impression that the target website is naturally earning links from multiple independent sources. If done effectively, this can temporarily improve keyword rankings, increase organic visibility, and drive more traffic from search results.

Jasa Backlink

Download Anime Batch