環境

ruby -v
ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [x86_64-linux]

 

rails -v
Rails 8.0.1

 

Railsアプリを作成する

rails new store -d mysql

cd store

 

データベースを作成する

config/database.ymlを調整する

rails db:create

 

Webサーバの設定を変更して、再起動アクセスする

Webサーバ設定変更

config/application.rbに以下を追加

 config.hosts << storeのFQDN

 

 

データベースモデルを作成する

bin/rails generate model Product name:string
bin/rails db:migrate
bin/rails generate controller Products index


ルーティングを編集する

config/routes.rbに以下を追加する

 

  root "products#index"
  resources :products

 

コントローラーとビューを作成する

app/controllers/products_controller.rb
 
class ProductsController < ApplicationController
  def index
    @products = Product.all
  end
end
 
app/views/products/index.html.erb
<%= debug @products %>