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

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

新規登録して質問してみよう
ただいま回答率
85.48%
Sequel Pro

Sequel Proは、Mac OS X向けMySQLフロントエンドです。GUIからのMySQLの操作・管理が可能になります。強力なクエリ編集、多彩なエンコーディングオプションのサポートなど多くの機能を備えています。

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

Q&A

解決済

1回答

1635閲覧

Couldn't find Prototype with 'id'=2

chibi0310mayu

総合スコア3

Sequel Pro

Sequel Proは、Mac OS X向けMySQLフロントエンドです。GUIからのMySQLの操作・管理が可能になります。強力なクエリ編集、多彩なエンコーディングオプションのサポートなど多くの機能を備えています。

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

0グッド

0クリップ

投稿2021/04/08 02:49

編集ページに移動して編集ができるか確認したい

発生している問題・エラーメッセージ

ActiveRecord::RecordNotFound in PrototypesController#edit Couldn't find Prototype with 'id'=2 Extracted source (around line #31): def edit binding.pry @prototype = Prototype.find(params[:id]) unless user_signed_in? && current_user.id == @prototype.user_id redirect_to action: :index Rails.root: /Users/nakamatsumayumi/projects/protospace-35188 Application Trace | Framework Trace | Full Trace app/controllers/prototypes_controller.rb:31:in `edit'

該当のソースコード

ruby

1class PrototypesController < ApplicationController 2 before_action :authenticate_user!, only: [:new, :edit, :destroy] 3 4 def index 5 @prototypes = Prototype.all 6 end 7 8 def new 9 @prototype = Prototype.new 10 11 end 12 13 def create 14 @prototype = Prototype.new(prototype_params) 15 if @prototype.save 16 redirect_to root_path 17 else 18 render :new 19 end 20 end 21 22 def show 23 @prototype = Prototype.find(params[:id]) 24 @comment = Comment.new 25 @comments = @prototype.comments.includes(:user) 26 27 end 28 29 def edit 30 @prototype = Prototype.find(params[:id]) 31 32 unless user_signed_in? && current_user.id == @prototype.user_id 33 redirect_to action: :index 34 end 35 36 end 37 38 def update 39 @prototype = Prototype.find(params[:id]) 40 41 if @prototype.update(prototype_params) 42 redirect_to prototype_path 43 else 44 render :edit 45 end 46 end 47 48 def destroy 49 prototype = Prototype.find(params[:id]) 50 prototype.destroy 51 redirect_to root_path 52 end 53 54 55 56 private 57 def prototype_params 58 params.require(:prototype).permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) 59 end 60end 61

ruby

1prototypes/show.html.erb 2 3<main class="main"> 4 <div class="inner"> 5 <div class="prototype__wrapper"> 6 <p class="prototype__hedding"> 7 <%= @prototype.title%> 8 </p> 9 <%= link_to "by#{@prototype.user.name}", user_path(@prototype.user.id), class: :prototype__user %> 10 <%if user_signed_in? && current_user.id == @prototype.user_id %> 11 <div class="prototype__manage"> 12 <%= link_to "編集する", edit_prototype_path(@prototype.user.id), class: :prototype__btn %> 13 <%= link_to "削除する", prototype_path(@prototype.id), method: :delete, class: :prototype__btn %> 14 <% end %> 15 </div> 16 <%# // プロトタイプの投稿者とログインしているユーザーが同じであれば上記を表示する %> 17 <div class="prototype__image"> 18 <%= image_tag @prototype.image %> 19 </div> 20 <div class="prototype__body"> 21 <div class="prototype__detail"> 22 <p class="detail__title">キャッチコピー</p> 23 <p class="detail__message"> 24 <%= @prototype.catch_copy %> 25 </p> 26 </div> 27 <div class="prototype__detail"> 28 <p class="detail__title">コンセプト</p> 29 <p class="detail__message"> 30 <%= @prototype.concept %> 31 </p> 32 </div> 33 </div> 34 <div class="prototype__comments"> 35 <%# ログインしているユーザーには以下のコメント投稿フォームを表示する %> 36 <% if user_signed_in? %> 37 <div class="field"> 38 <%= form_with(model: [@prototype, @comment], local: true) do |f| %> 39 <%= f.label :text, "コメント" %><br /> 40 <%= f.text_field :text, id:"comment_text" %> 41 </div> 42 <div class="actions"> 43 <%= f.submit "送信する", class: :form__btn %> 44 </div> 45 <% end %> 46 <% end %> 47 <%# // ログインしているユーザーには上記を表示する %> 48 <ul class="comments_lists"> 49 <%# 投稿に紐づくコメントを一覧する処理を記述する %> 50 <% @comments.each do | comment |%> 51 <li class="comments_list"> 52 <%= comment.text %> 53 <%= link_to comment.user.name, user_path(@prototype.user.id), class: :comment_user %> 54 </li> 55 <% end %> 56 57 <%# // 投稿に紐づくコメントを一覧する処理を記述する %> 58 </ul> 59 </div> 60 </div> 61 </div> 62</main> 63

ruby

1Rails.application.routes.draw do 2 devise_for :users 3 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html 4 root to: "prototypes#index" 5 resources :prototypes do 6 resources :comments, only: :create 7 end 8 resources :users, only: :show 9 10end 11

試したこと

binding.pry
@prototype = Prototype.find(params[:id])
をしてここに問題があることはわかりました
Sequel proで確認したところ protypeのidの2は存在しなっかたのでエラーがでているとわかったのですが
削除してしまって消えていると思います。
deviseをつかってログイン機能とprototypeを紐づけているのですが
idの取得方法が違うのかと思いいろいろ試しましたがうまくいかず

補足情報(FW/ツールのバージョンなど)

ruby
ruby on rails 6.0.0.
devese

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

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

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

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

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

maisumakun

2021/04/08 02:51

どうやってその編集ページにアクセスしようとしましたか?
chibi0310mayu

2021/04/08 02:54

詳細画面から編集ページへアクセスしました!! もしかして詳細画面の編集pathの方がおかしいですか??
maisumakun

2021/04/08 02:57

> もしかして詳細画面の編集pathの方がおかしいですか?? そのとおりですね。「存在しない案件の編集ページ」にリンクしてしまっている側の問題です。
chibi0310mayu

2021/04/08 03:00

ありがとうございます!!引数を@prototype.idに変更してみたら大丈夫でした!! userをいれてしまっていたのでダメだったのですね 助かりました!!ありがとうございます!!
gouf

2021/04/09 15:17

質問が解決した場合、内容をまとめて自己回答し、それをベストアンサーに設定することで、この質問を解決済みにすることができます
guest

回答1

0

自己解決

<%= link_to "編集する", edit_prototype_path(@prototype.user.id), class: :prototype__btn %

を 

<%= link_to "編集する", edit_prototype_path(@prototype.id), class: :prototype__btn %

に変更しました。

投稿2021/04/10 08:37

chibi0310mayu

総合スコア3

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問