Kurzreferenz

Ruby on Rails Überblick Spickzettel

Ein Architektur-Spickzettel für Ruby on Rails: MVC-Konventionen, CLI-Befehle, Controller, Views, Layouts und Helper.

· Veröffentlicht

Ruby on Rails ist ein Full-Stack-Webanwendungs-Framework, das dem MVC-Muster (Model-View-Controller) und dem Prinzip "Convention over Configuration" folgt.

Teil-Spickzettel in diesem Leitfaden


Häufige CLI-Befehle

# Neues Rails-Projekt mit PostgreSQL erstellen
rails new myapp --database=postgresql

# Entwicklungsserver auf localhost:3000 starten
bin/rails server

# Interaktive Rails-Konsole starten
bin/rails console

# Datenbank-Migrationen ausführen
bin/rails db:migrate

Controller- & Action-Konventionen

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 & Yielding

In 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>

Referenzen

Verwandte Themen