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
- Cheat Sheet Modelli ActiveRecord Rails
- Cheat Sheet Controller Rails
- Cheat Sheet Routing Rails
- Cheat Sheet Migrazioni Database Rails
- Cheat Sheet Helper dei Form Rails
- Cheat Sheet Helper delle Viste Rails
- Cheat Sheet Internazionalizzazione (i18n) Rails
- Cheat Sheet Trucchi e Suggerimenti Rails
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>