Riferimento rapido

Cheat Sheet Panoramica Ruby on Rails

Guida panoramica all'architettura di Ruby on Rails: convenzioni MVC, comandi da riga di comando, controller, viste, layout ed helper.

· Pubblicato

Ruby on Rails è un framework full-stack per applicazioni web basato sul pattern Model-View-Controller (MVC) e sul principio "Convention over Configuration".

Schede di riferimento incluse in questa guida


Comandi CLI comuni

# Genera un nuovo progetto Rails con PostgreSQL
rails new myapp --database=postgresql

# Avvia il server di sviluppo su localhost:3000
bin/rails server

# Avvia la console interattiva di Rails
bin/rails console

# Esegui le migrazioni del database
bin/rails db:migrate

Convenzioni di controller ed azioni

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

Layout e Yielding

Nel file 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>

Riferimenti

Argomenti correlati