###前提・実現したいこと
controllerのedit機能についてですが、レコードのidを取得して、内容を編集できるようにしたいのですが、idを取得することができません。
インスタンス変数を作り、モデルと結びつけ、paramsでidを取得できると思うのですが、どこを直せば良いのでしょうか?
RUby
1class ProductsController < ApplicationController 2 def index 3 @products = Product.all 4 end 5 def new 6 @product = Product.new 7 end 8 def create 9 product = Product.new(product_params) 10 product.save 11 redirect_to :action =>"new" 12 end 13 14 def destroy 15 product = Product.find(params[:id]) 16 product.destroy 17 redirect_to :action =>"index" 18 end 19 20 def edit 21 @products = Product.find(params[:id]) 22 end 23 24 25 def update 26 product = Product.find(params[:id]) 27 product.update_attributes(product_params) 28 redirect_to :action =>"index" 29 end 30 31 def product_params 32 params.permit(:maker, :price, :use, :characteristic, :material, :color, 33 :method, :industry, :support, :size, :image, :agency) 34 end 35 36end
###試したこと
エラーの内容です。
Ruby
1ActiveRecord::RecordNotFound in ProductsController#edit 2Couldn't find Product with 'id'=#<Product::ActiveRecord_Relation:0x007fd122deb700> 3 4Extracted source (around line #24): 522 623 724 825 926 1027 11 12 13 def edit 14 @products = Product.find(params[:id]) 15 end 16 17 18
初心者なので、どうしたら良いのかよくわからないので、詳しく教えていただけると嬉しいです。
###補足情報(言語/FW/ツール等のバージョンなど)
railsバージョン 4.2.6
記載しておらず、大変申し訳ございません。
こちらがルートの設定です。
Rails.application.routes.draw do root 'products#index' resources :products end コード
ルートファイルの設定はどうなってますか?
回答1件
あなたの回答
tips
プレビュー