Referência rápida

Guia Rápido de Visão Geral do Ruby on Rails

Guia arquitetural de alto nível para Ruby on Rails: convenções MVC, comandos CLI, controllers, views, layouts e helpers.

· Publicado

O Ruby on Rails é um framework web full-stack que segue o padrão Model-View-Controller (MVC) e o princípio "Convenção sobre Configuração".

Sub-guias incluídos nesta referência


Comandos CLI Comuns

# Criar novo projeto Rails com PostgreSQL
rails new myapp --database=postgresql

# Iniciar servidor de desenvolvimento em localhost:3000
bin/rails server

# Iniciar console interativo do Rails
bin/rails console

# Executar migrações do banco de dados
bin/rails db:migrate

Convenções de Controllers e Actions

class ProjectsController < ApplicationController
  before_action :set_project, only: [:show, :edit, :update, :destroy]

  def index
    @projects = Project.all
  end

  def show
  end

  private

  def set_project
    @project = Project.find(params[:id])
  end
end

Layouts e Yielding

Em app/views/layouts/application.html.erb:

<!doctype html>
<html>
  <head>
    <title><%= content_for?(:title) ? yield(:title) : "Default App" %></title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>
    <%= stylesheet_link_tag "application" %>
    <%= javascript_importmap_tags %>
  </head>
  <body>
    <%= yield %>
  </body>
</html>

Referências

Temas relacionados