[英]pg_search with nested routes
我是第一次安裝pg_search,並且正在嘗試搜索書籍和章節。 這些是嵌套路線。
routes.rb中:
resources :books do
resources :chapters, except: [:index]
end
pgsearch結果顯示了該信息的鏈接,但是章節鏈接顯示了/ 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
如何獲得正確的路線?
我認為您的link_to
路徑缺少父對象以形成正確的url。 因為路由器希望使用一個books/:id/chapters/:id
形式的URL,所以我們還需要傳遞link_to
作為章節一本書對象。
嘗試這個:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.