ActionController :: UrlGenerationError: missing required keys: [: id]

I run RSpec 3 tests and get the same error for this one specific path:

 Failure/Error: visit book_path
 ActionController::UrlGenerationError:
 No route matches {:action=>"show", :controller=>"books"} missing required keys: [:id]

My test is not completely finished, so I am sure that some of the latter may be incorrect. But I can not start my line of visit path:

...
book = FactoryGirl.build(:book)
reviews = FactoryGirl.build_list(:review, 5, book: book)

visit book_path

reviews.each do |review|
  expect(page).to have_content(review.rating)
  expect(page).to have_content(review.body)
  expect(page).to have_content(review.timestamp)
end

  expect(page).to have_content('House of Leaves')
  expect(page).to have_content('Mark Z. Danielewski')
  expect(page).to have_content('Horror')
end

In my controller, I show the following:

def show
  @book = Book.find(params[:id])
  @reviews = @book.reviews.order('created_at DESC')
  @review = Review.new
end

And my resources:

resources :books, only: [:index, :show, :new, :create] do
  resources :reviews, only: [:show, :new, :create] do
end
+1
source share
1 answer

This causes an error because you did not determine which book you would like to visit. Invalid key idis the book id. Therefore, you should change this to:

book = FactoryGirl.build(:book)
reviews = FactoryGirl.build_list(:review, 5, book: book)

visit book_path(book)

, (book_path(book.id)), Rails, .

+6

All Articles