NoMethodError in Books#index


#1

I have completed all the code but when I run the project, I get an error like this…

This is the HTML file (app/views/books/index.html.erb) that generates the error…

<% @books.each do |book| %>
  <div class="book">
    <div class="row">
      <div class="col-md-1">
        <p class="rank-this-week"> <%= book.rank_this_week %> </p>
      </div>
      <div class="col-md-9">
        <p class="title"> <%= book.title %> </p>
        <p class="author">by  <%= book.author %></p>
        <p class="publisher">(<%= book.publisher %>)</p>
        <p class="description"> <%= book.description %> </p>
        <%= link_to 'See all Editorial Reviews', book %>
      </div>
      <div class="col-md-2">
        <p class="weeks-on-list"> <%= book.weeks_on_list %> weeks on list</p>
      </div>
    </div>
  </div>
<% end %>

This is my books_controller.rb

class BooksController < ApplicationController
  def index 
    @books = Book.all
  end
  
  def show
  	@book = Book.find(params[:id])
  	@reviews = @book.reviews
	end
end

And this is my routes.rb file

Rails.application.routes.draw do
  resource :books
  root 'books#index'
end

#2

Hi @terawhiz79906,

Try linking to the book with book_url(book) instead:

<%= link_to 'See all Editorial Reviews', book_url(book) %>

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.