0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Ruby/Rails学習記録】Day 3:MVC構造とコマンドのまとめ

Posted at

【Ruby/Rails学習記録】Day 3:MVC構造とコマンドのまとめ
📌 本日の学習内容
静的/動的コンテンツの違い

クライアントサイドとサーバーサイドの役割

SinatraとRailsにおけるルーティングの概要

HTTPメソッド(GET, POST, PUT, DELETE)

RailsのMVC構造と各役割

よく使うRailsコマンド一覧

📂 RailsのMVC構造まとめ(図付き)

┌────────────┐
│ ブラウザ │ ← クライアントサイド (HTML, CSS, JS)
└─────┬──────┘
↓ HTTPリクエスト
┌─────▼──────┐
│ Controller │ ← リクエストを受け取り処理を決定
└─────┬──────┘
↓ モデルにアクセス
┌─────▼──────┐
│ Model │ ← データの取得・保存・バリデーション
└─────┬──────┘
↓ 結果を渡す
┌─────▼──────┐
│ View │ ← ユーザーに見えるHTMLを生成(ERB)
└────────────┘

HTMLとしてレスポンスを返す

🛠 よく使うRailsコマンド一覧

コマンド 説明
rails new アプリ名 新しいRailsアプリケーションを作成
rails s 開発用Webサーバー(Puma)を起動
rails c Railsコンソールを起動(IRBより便利)
rails g controller コントローラー名 コントローラーと対応するファイルを生成
rails d controller コントローラー名 コントローラーと関連ファイルを削除
rails g model モデル名 カラム名:型 モデルとマイグレーションファイルを生成
rails db:create データベースを作成(初回に使用)
rails db:migrate マイグレーションファイルを元にDBを更新
rails db:rollback 直前のマイグレーションを取り消す
rails routes 現在のルーティング一覧を表示

💡補足
.erb ファイルは View の一部で、HTMLにRubyを埋め込むテンプレートです。
@変数名 のように「@」が付くのはインスタンス変数。ビューで使えるのもこれです。
モデル.save はバリデーション後にデータベースへ保存する操作です。

🚏 ルーティングの詳しい書き方(Rails)
🔸 基本構文(config/routes.rb)

Rails.application.routes.draw do

トップページ

root "home#index"

単一ルート(GETメソッド)

get "about", to: "pages#about"

リソースベースのルーティング(7つの基本アクション)

resources :articles
end

アクション名 HTTPメソッド パス 対応する処理(コントローラー#アクション)
index GET /articles articles#index
show GET /articles/:id articles#show
new GET /articles/new articles#new
create POST /articles articles#create
edit GET /articles/:id/edit articles#edit
update PATCH/PUT /articles/:id articles#update
destroy DELETE /articles/:id articles#destroy

🧱 MVC構造のコードとディレクトリ関係
Railsのフォルダ構成(一部抜粋)
app/
├── controllers/
│ └── articles_controller.rb ← コントローラー(処理を書く)
├── models/
│ └── article.rb ← モデル(データとロジック)
├── views/
│ └── articles/
│ ├── index.html.erb ← 一覧ページ
│ ├── show.html.erb ← 詳細ページ
│ ├── new.html.erb ← 新規作成フォーム
│ └── edit.html.erb ← 編集フォーム
config/
└── routes.rb ← URLとアクションを結びつける

✍ コントローラーのコード例(app/controllers/articles_controller.rb)
class ArticlesController < ApplicationController
def index
@articles = Article.all # モデルを使ってデータを取得
end

def show
@article = Article.find(params[:id]) # 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

private

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

✍ モデルのコード例(app/models/article.rb)
class Article < ApplicationRecord
validates :title, presence: true
validates :body, presence: true
end

✍ ビュー(例: app/views/articles/index.html.erb)

記事一覧

<% @articles.each do |article| %>

<%= link_to article.title, article_path(article) %>

<%= article.body.truncate(100) %>

<% end %>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?