[英]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.