前提
RailsでAPIを作成しています
seeds.rbとは. 初期データを生成して、
localhost:3000/restaurants/xxx
でjsonのid:"xxx"の配列を取得したいのですが、上手くいきません
localhost:3000/restaurants
で初期データの全体の配列は取得できています
発生している問題・エラーメッセージ
No route matches [GET] "/restaurants/1" Rails.root: /app
該当のソースコード
routes.rb
ruby
1Rails.application.routes.draw do 2 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 3 4 resources :restaurants, only: %i[index] 5end
restaurants_controller.rb
ruby
1class RestaurantsController < ActionController::Base 2 def index 3 restaurants = Restaurant.all 4 5 render json: { data: restaurants } 6 end 7 8 def show 9 restaurants = Restaurant.find(params[:id]) 10 11 render json: { data: restaurants } 12 end 13end
20221222054903_create_restaurant.rb
ruby
1class CreateRestaurant < ActiveRecord::Migration[5.2] 2 def change 3 create_table :restaurants do |t| 4 t.string :name, null: false 5 t.text:description 6 t.integer :price 7 t.text :thumbnail_url, null: false 8 9 t.timestamp 10 end 11 end 12end
試したこと
初歩的な質問で申し訳ないのですが、ぜひよろしくお願いいたします。

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2022/12/23 03:26
2022/12/23 03:30