Ruby on Rails Cycles

Explore the power of Ruby on Rails loops, with an example of a loop to print numbers 1 to 10.

The MVC Pattern

Ruby on Rails is built on the Model-View-Controller (MVC) pattern. It is an architectural pattern that separates an application into three main logical components: the model, the view, and the controller. The model is responsible for maintaining data and business logic. The view is responsible for displaying all or a portion of the data to the user. The controller is responsible for responding to user input and performs tasks by making calls to the model and the view.

In the MVC pattern, a request is sent to the controller. The controller communicates with the model to retrieve or update data as needed. It then passes the data to the view which renders the page. The view then sends the finished page back to the controller which sends it to the user's browser.

Routing

Routing is the process of determining the controller and action for a given URL. It is usually done by matching the URL against a series of regular expressions. When a match is found, the associated controller and action are called. The controller then performs the necessary tasks and passes the data to the view which renders the page.


Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  get '/', to: 'pages#home'
  get 'about', to: 'pages#about'
  resources :articles
end

In the above example, the root URL (/) is mapped to the home action in the pages controller. The about URL is mapped to the about action in the same controller. The resources :articles line tells Rails to create all the routes for the articles resource. This includes the routes for creating, reading, updating, and deleting articles.

Controllers

Controllers are responsible for responding to user input and performing tasks. They are the middleman between the model and the view. They handle the incoming requests, retrieve and manipulate data, and create the response that is sent back to the user.


class ArticlesController < ApplicationController
  def index
    @articles = Article.all
  end

  def show
    @article = Article.find(params[:id])
  end

  def new
    @article = Article.new
  end

  def create
    @article = Article.new(article_params)

    if @article.save
      redirect_to @article
    else
      render 'new'
    end
  end

  def edit
    @article = Article.find(params[:id])
  end

  def update
    @article = Article.find(params[:id])

    if @article.update(article_params)
      redirect_to @article
    else
      render 'edit'
    end
  end

  def destroy
    @article = Article.find(params[:id])
    @article.destroy

    redirect_to articles_path
  end

  private
    def article_params
      params.require(:article).permit(:title, :text)
    end
end

In the above example, the ArticlesController defines the seven actions needed for a basic CRUD (Create, Read, Update, and Delete) interface. The index action retrieves all the articles from the database and assigns them to an instance variable. The show action retrieves a single article from the database and assigns it to an instance variable. The new action creates a new article object and assigns it to an instance variable. The create action creates a new article record in the database. The edit action retrieves an article object from the database and assigns it to an instance variable. The update action updates an existing article record in the database. The destroy action deletes an article record from the database. Finally, the private section contains a method to whitelist the parameters that are allowed to be passed in.

Views

Views are responsible for displaying the data to the user. They are written in Embedded Ruby (ERB) which is a templating language that mixes HTML and Ruby code. The data passed to the view from the controller is available to the view as instance variables.


<h1><%= @article.title %></h1>
<p><%= @article.text %></p>

In the above example, the view is displaying the title and text of an article. The @article instance variable is set in the controller and is then available in the view. The ERB syntax is used to embed the title and text into the HTML.

Conclusion

Ruby on Rails is a powerful web framework that follows the MVC pattern. It is composed of three main components: the model, the view, and the controller. Routing is used to determine which controller and action to call for a given URL. Controllers are responsible for responding to user input and performing tasks. Views are responsible for displaying the data to the user. Together, the MVC components form the foundation of a Rails application.

Answers (0)