質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.45%
Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

608閲覧

ActiveRecord::RecordNotFound in のエラー

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

1グッド

1クリップ

投稿2021/08/16 09:53

編集2021/08/16 13:34

テーブル名
・posts
・itemss
1(posts):多(items)の関係です。

itemsには
・item_image
・brand
・size
・price
・products_name
・post_id
・user_id
以上の7つのカラムがあります。

エラー内容

イメージ説明

html

1items.controller.erb 2class ItemsController < ApplicationController 3 def index 4 @posts = Post.all 5 @user = current_user 6 @items = @user.items 7 #@userの子であるitemに限定する。 8 end 9 10 def new 11 @post = Post.find_by(id: params[:id]) 12 @item = Item.new(item_image: params[:item_image], brand: params[:brand], size: params[:size], price: params[:price], products_name: params[:products_name], post_id: params[:post_id]) 13 end 14 15 def create 16 @item = Item.new(item_image: params[:item_image], brand: params[:brand], size: params[:size], price: params[:price], products_name: params[:products_name], post_id: params[:post_id]) 17 @item.user_id = current_user.id 18 if @item.save 19 @item.item_image="#{@item.id}.jpg" 20 image=params[:item_image] 21 File.binwrite("app/assets/images/items/#{@item.item_image}", image.read) 22 redirect_to("/items/#{@item.post_id}/show") 23 else 24 render("/items/new") 25 end 26 end 27 28 def show 29 @item = Item.find_by(params[:id]) 30 @post = Post.find_by(id: params[:id]) 31 @items = @post.items 32 33 end 34 35 def edit 36 @item = Item.find_by(id: params[:id]) 37 end 38 39 def destroy 40 @item = Item.find(params[:id])            ←エラー箇所 41 @item.destroy 42 redirect_to("/posts/#{@post.id}") 43 end 44end

html

1vews/items/show.html.erb 2 3<div class="main posts-show"> 4 <div class="container"> 5 <div class="posts-show-item"> 6 <% @items.each do |item| %> 7 <tr> 8 <% if File.exist?("app/assets/images/items/#{item.id}.jpg") %> 9 <td><%= image_tag "items/#{item.id}" %></td> 10 <% else %> 11 <td><%= image_tag "no_image.png" %></td> 12 <% end %> 13 <tr> 14 <!--アイテム画像--> 15 <td></td> 16 </tr> 17 <tr> 18 <!--アイテム名--> 19 <td><%= item.products_name %></td> 20 </tr> 21 <tr> 22 <!--ブランド--> 23 <td><%= item.brand %></td> 24 </tr> 25 <tr> 26 <!--サイズ--> 27 <td><%= item.size %></td> 28 </tr> 29 <tr> 30 <!--価格--> 31 <td><%= item.price %></td> 32 </tr> 33 <% end %> 34 35 <div class="post-time"> 36 <%= @item.created_at.strftime("%Y-%m-%d") %> 37 </div> 38 <% if @post.user_id == @current_user.id %> 39 <div class="post-menus"> 40 <%= link_to("アイテム編集","/items/#{@post.id}/edit") %> 41 <%= link_to("アイテム削除", "/items/#{@item.post_id}/destroy", {method: "post"}) %> 42 </div> 43 <% end %> 44 <%= link_to("コーディネート画面へ戻る", "/posts/#{@post.id}") %> 45 </div> 46 </div> 47 <%= link_to("トップページへ", "/") %> 48</div>

このように記載し、vews/items/show.html.erbの
<%= link_to("アイテム削除", "/items/#{@item.post_id}/destroy", {method: "post"}) %>
このlinkを踏んだ際に出るエラーとなります。
イメージ説明
今回はpost_idの159番を削除したいのですがカラムには番号が入っています。
postsのidとitemsのpost_idは紐付けています。

159番のアイテムを消去しようとするとidがないと、正常に削除できません。

何かアドバイスや原因などわかる方いらっしゃいましたらコメントいただけると幸いです、、
よろしくお願い致します。

shinoharat👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

winterboum

2021/08/16 13:04

show のcodeを載せてください
退会済みユーザー

退会済みユーザー

2021/08/16 13:35

winterboum様 追記いたしました。よろしくお願い致します。
guest

回答2

0

def show @item = Item.find_by(params[:id]) @post = Post.find_by(id: params[:id])

@item も @post も同じパラメータの値を使ってます。
なので、@postが得られていません。@postはpostのidを使ってください

投稿2021/08/16 20:49

winterboum

総合スコア23420

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2021/08/17 15:28

ご回答ありがとうございます。 @postの定義を @post = Post.find(@item.post.id) としたのですが異なる番号のアイテムが削除されてしまいました、、 (169を削除しようとすると167が消える)
winterboum

2021/08/17 22:19

それは 169だと思っていた @item.post が 167だったのでは?
guest

0

ベストアンサー

エラーの原因

view のアイテム削除のリンクに使っている id は Post の id ですが、

html

1<%= link_to("アイテム削除", "/items/#{@item.post_id}/destroy", {method: "post"}) %>

destory アクションではその id を使って Item を検索しています。

rb

1 def destroy 2 @item = Item.find(params[:id])

--

id=159 の Post は存在しますが、
id=159 の Item は存在しません。
このコードだと id=159 の Item を検索するので、当然エラーになります。

修正方法

もし Item を削除したいなら、

【STEP 1】
winterboum さんの指摘の通り、show アクションで @item も @post も同じ id を使っている問題を修正

diff

1app/controllers/item_controller.rb 2 3 def show 4- @item = Item.find_by(params[:id]) 5- @post = Post.find_by(id: params[:id]) 6+ @item = Item.find(params[:id]) 7+ @post = @item.post 8 @items = @post.items 9 10 end

--

【STEP 2】
「アイテム編集」「アイテム削除」のリンクでは Item の id を利用する。

diff

1app/views/items/show.html.erb 2 3 <div class="post-menus"> 4- <%= link_to("アイテム編集","/items/#{@post.id}/edit") %> 5- <%= link_to("アイテム削除", "/items/#{@item.post_id}/destroy", {method: "post"}) %> 6+ <%= link_to("アイテム編集","/items/#{@item.id}/edit") %> 7+ <%= link_to("アイテム削除", "/items/#{@item.id}/destroy", {method: "post"}) %> 8 </div>

--

【STEP 3】

パラメータの id を使って Item を削除する。(ここは修正なし)

ただし、その直後のリダイレクト用に post_id の取得が必要です。

diff

1app/controllers/item_controller.rb 2 3 def destroy 4 @item = Item.find(params[:id]) 5 6+ # このあと Post の一覧画面にリダイレクトしたいので、削除前に post_id を取得しておく 7+ post_id = @item.post_id 8 9 @item.destroy 10 11- redirect_to("/posts/#{@post.id}") 12+ redirect_to("/posts/#{post_id}") 13 end

投稿2021/08/26 04:50

編集2021/08/26 04:54
shinoharat

総合スコア1685

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.45%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問