I installed pg_search for the first time and I'm trying to create a search for Books and Chapters. These are nested routes.
routes.rb:
resources :books do
resources :chapters, except: [:index]
end
pgsearch results display the links to the information but the chapters link shows /chapters/17, when it should display /books/50/chapters/17.
search index view:
<h2>
<% @pg_searches.each do |pg_search| %>
<p> <%= link_to pg_search.searchable.title, pg_search.searchable %> </p>
<% end %>
<h2>
SearchesController
class SearchesController < ApplicationController
def index
@pg_searches = PgSearch.multisearch(params[:query])
end
end
chapter.rb
include PgSearch
multisearchable :against => [:title, :body]
book.rb
include PgSearch
multisearchable :against => [:title, :description]
Here's the error message:
Couldn't find Book without an ID
def show
**@book = Book.find(params[:book_id])**
@chapters = Chapter.all
@chapter = Chapter.find(params[:id])
@table_of_contents = @chapter.table_of_contents
How do I get the correct routes?
I think your link_to
path is missing the parent object in order to form the proper url. Because the router wants a URL of the form books/:id/chapters/:id
, we also need to pass link_to
for chapter a book object.
Try this: