繁体   English   中英

pg_search与嵌套路由

[英]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作为章节一本书对象。

尝试这个:

https://gist.github.com/MichaelCPell/08fb6af5c27b0466366f

我认为,您需要尝试搜索关联或检查类似的问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM