【问题标题】:pg_search with nested routes带有嵌套路由的 pg_search
【发布时间】:2015-11-05 17:41:10
【问题描述】:

我是第一次安装 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>

搜索控制器

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

如何获得正确的路线?

【问题讨论】:

    标签: ruby-on-rails pg-search


    【解决方案1】:

    我认为您的 link_to 路径缺少父对象以形成正确的 url。因为路由器需要 books/:id/chapters/:id 形式的 URL,我们还需要为章节对象传递 link_to

    试试这个:

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

    【讨论】:

      【解决方案2】:

      我想,你需要试试searching through associations 或查看类似的question

      【讨论】:

      • 尝试了 associated_against,仍然收到相同的结果。
      猜你喜欢
      • 2017-07-23
      • 2017-09-23
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 2017-05-19
      • 1970-01-01
      • 2018-03-15
      • 2017-10-05
      相关资源
      最近更新 更多