投稿のshowページへのリンク切替え時に以下のエラーが発生しております。
ActionController::UrlGenerationError in Books#show Showing /home/ec2-user/environment/bookers-level2.herokuapp/app/views/books/show.html.erb where line #42 raised: No route matches {:action=>"edit", :controller=>"books", :id=>nil} missing required keys: [:id] Extracted source (around line #42): 40 <td><%= @book.title %></td> 41 <td><%= @book.body %></td> 42 <td><%= link_to "Edit", edit_book_path(@book), class: "btn btn-success" %></td> 43 <td><%= link_to "Delete", book_path(@book), class: "btn btn-danger", method: :delete %></td> 44 </tr> 45 </tbody>
link_to "Delete", book_path(@book)の引数は間違っていないと思うのですが、エラーになってしまい解決できません。
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 @user = User.find(params[:id]) @book = Book.new 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
books/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' %>
お気づきの点がありましたらご指摘をお願い致します。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。