pg_search 带有嵌套路由
pg_search with nested routes
我是第一次安装 pg_search,我正在尝试创建 Books and Chapters 搜索。这些是嵌套路线。
routes.rb:
resources :books do
resources :chapters, except: [:index]
end
pgsearch 结果显示 link 的信息,但章节 link 显示 /chapters/17,而它应该显示 /books/50/chapters/17。
搜索索引视图:
<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]
错误信息如下:
找不到没有 ID 的图书
def show
**@book = Book.find(params[:book_id])**
@chapters = Chapter.all
@chapter = Chapter.find(params[:id])
@table_of_contents = @chapter.table_of_contents
如何获得正确的路线?
我想,你需要试试searching through associations
或检查类似的 question
我认为您的 link_to
路径缺少父对象以形成正确的 url。因为路由器想要一个 books/:id/chapters/:id
形式的 URL ,我们还需要为章节 a book 对象传递 link_to
。
试试这个:
我是第一次安装 pg_search,我正在尝试创建 Books and Chapters 搜索。这些是嵌套路线。
routes.rb:
resources :books do
resources :chapters, except: [:index]
end
pgsearch 结果显示 link 的信息,但章节 link 显示 /chapters/17,而它应该显示 /books/50/chapters/17。
搜索索引视图:
<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]
错误信息如下:
找不到没有 ID 的图书
def show
**@book = Book.find(params[:book_id])**
@chapters = Chapter.all
@chapter = Chapter.find(params[:id])
@table_of_contents = @chapter.table_of_contents
如何获得正确的路线?
我想,你需要试试searching through associations 或检查类似的 question
我认为您的 link_to
路径缺少父对象以形成正确的 url。因为路由器想要一个 books/:id/chapters/:id
形式的 URL ,我们还需要为章节 a book 对象传递 link_to
。
试试这个: