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

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

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

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

Q&A

解決済

2回答

1505閲覧

Couldn't find Book with 'id'=2を解決したい(Rails)

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails

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

0グッド

0クリップ

投稿2019/12/08 18:45

編集2019/12/09 08:04
ActiveRecord::RecordNotFound in BooksController#show Couldn't find Book with 'id'=2 Extracted source (around line #16): 14 15 def show 16 @book = Book.find(params[:id]) 17 end 18 19 def edit

このようなエラーが発生しております。
ログイン後に行っており投稿などは正常に表示できているのですが、詳細ページへの移動でエラーになってしまいます。
何かお気づきの点がありましたらご指摘お願いします。

show.html.erb <%= render 'shared/header' %> <div class="top"> <div class="container"> <div class="row"> <div class="col-lg-3"> <p>User info</p> <%= attachment_image_tag @user, :profile_image, :fill, 150, 150, format: 'jpeg', fallback: "no_image.jpg", size:'150x150' %> <table class="table"> <tr>    <th>name</th>    <th><%= @user.name %></th>   </tr>   <tr>    <th>introduction</th>    <th><%= @user.introduction %></th>    </tr> </table> <%= link_to edit_user_path, class: "btn btn-default" do %> <i class="fa fa-wrench"></i> <% end %> <p>New book</p> <%= form_for @book do |f| %> <p>Title</p> <%= f.text_field :title %> <p>Opinion</p> <%= f.text_area :body %> <%= f.submit 'Create Book' %> <% end %> </div> <div class="col-lg-9"> <p>Book detail</p> <table class="table table-hover"> <tbody> <tr> <td><%= attachment_image_tag @user, :profile_image, :fill, 50, 50, format: 'jpeg', fallback: "no_image.jpg", size:'50x50' %><br><%= @user.name %></td> <td><%= book.title %></td> <td><%= book.body %></td> <td><%= link_to "Edit", edit_book_path(@book), class: "btn btn-success" %></td> <td><%= link_to "Delete", book_path(@book), class: "btn btn-danger", method: :delete %></td> </tr> </tbody> </table> </div> </div> </div> </div> <%= render 'shared/footer' %>
books_controller.rb class BooksController < ApplicationController before_action :authenticate_user!, only: [:show] def create book = Book.new(book_params) book.user_id = current_user.id book.save! redirect_to user_url(current_user.id) end def index @book = Book.page(params[:book]).reverse_order end def show @book = Book.find(params[:id]) end def edit @book = Book.find(params[:id]) end def update book = Book.find(params[:id]) book.update(book_params) redirect_to user_url(current_user.id) end def destroy @book = Book.find(params[:id]) @book.destroy redirect_to end private def book_params params.require(:book).permit(:title, :body) end end
$ rake routes Prefix Verb URI Pattern Controller#Action new_user_session GET /users/sign_in(.:format) users/sessions#new user_session POST /users/sign_in(.:format) users/sessions#create destroy_user_session DELETE /users/sign_out(.:format) users/sessions#destroy new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel new_user_registration GET /users/sign_up(.:format) users/registrations#new edit_user_registration GET /users/edit(.:format) users/registrations#edit user_registration PATCH /users(.:format) users/registrations#update PUT /users(.:format) users/registrations#update DELETE /users(.:format) users/registrations#destroy POST /users(.:format) users/registrations#create root GET / top#top home_about GET /home/about(.:format) top#home users GET /users(.:format) users#index POST /users(.:format) users#create new_user GET /users/new(.:format) users#new edit_user GET /users/:id/edit(.:format) users#edit user GET /users/:id(.:format) users#show PATCH /users/:id(.:format) users#update PUT /users/:id(.:format) users#update DELETE /users/:id(.:format) users#destroy books GET /books(.:format) books#index POST /books(.:format) books#create new_book GET /books/new(.:format) books#new edit_book GET /books/:id/edit(.:format) books#edit book GET /books/:id(.:format) books#show PATCH /books/:id(.:format) books#update PUT /books/:id(.:format) books#update DELETE /books/:id(.:format) books#destroy refile_app /attachments #<Refile::App app_file="/home/ec2-user/.rvm/gems/ruby-2.6.3/bundler/gems/refile-46b4178654e6/lib/refile/app.rb">

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

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

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

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

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

YukiMiyatake

2019/12/08 19:50

タイトルに言語、フレームワーク等を記入すると回答してくれやすいと思います
guest

回答2

0

ベストアンサー

詳細ページへの移動でエラーになってしまいます。

エラーメッセージのとおりで、当該idBookが見つからない状態です。

  • 手動でURLを入力した場合は、単なる入力ミスです。
  • コードで貼ったリンクで発生した場合は、リンク生成のミスです。

なお、このエラーは本番でも起こりうるものなので、rescue_fromを仕掛けておくことも必要かと思います。

投稿2019/12/08 22:14

maisumakun

総合スコア145121

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

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

退会済みユーザー

退会済みユーザー

2019/12/09 06:43

ご回答頂きありがとうございます。 手動で入力していないのでリンク生成時のミスだと思うのですが、どこを直せば良いかが分からなくなっております。 「@book = Book.find(params[:id])」と「link_to "Edit", edit_book_path(@book)」でidを引き渡せていないのでしょうか?
guest

0

質問タイトルに、言語やフレームワークを書いた方が回答を得られる気がします

投稿2019/12/08 19:42

YukiMiyatake

総合スコア144

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

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

退会済みユーザー

退会済みユーザー

2019/12/09 06:35

アドバイス頂きありがとうございます。 以後気を付けます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問