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">
タイトルに言語、フレームワーク等を記入すると回答してくれやすいと思います
回答2件
あなたの回答
tips
プレビュー