2013-10-16 21 views
1

我終於想出瞭如何實現pg_search的multisearch功能。如何將PGSeaerch結果鏈接到嵌套資源中的索引頁?

但是我在製作可用的搜索頁面時遇到了麻煩,該頁面顯示鏈接回到適當的嵌套頁面。

如何傳遞正確的ID以便我可以鏈接到嵌套的視圖。

因此,像這樣的link_to

myapp.com/artists/1/albums 

到目前爲止,我得到這個錯誤(我似乎無法通過藝術家ID)

No route matches {:controller=>"albums", :artists=>nil} 

新到Rails請幫助:)

控制器

class ResultsController < ApplicationController 

    def index 
    @results = PgSearch.multisearch(params[:query]).paginate(:page => params[:page]) 
    @artists = PgSearch.multisearch(params[:query]).where(searchable_type: "Artist") 
    @albums = PgSearch.multisearch(params[:query]).where(searchable_type: "Album") 
    end 

end 

VIEWS

<% if @results.any? %> 

<% if @artists.any? %> 

     <div class="maintitle">Artists</div> 

     <% @results.each do |pg_results| %> 
     <% if pg_results.searchable_type == "Artist" %> 
      <div class="span3"> 

      #### How can I link to the Albums index page 
      <%= link_to pg_results.searchable.name, 
       artist_albums_path(@artists) %> 


      </div>  
     <% end %> 
     <% end %> 

<% end %> 

<% if @albums.any? %> 

    <div class="maintitle">Albums</div> 

    <div class="row"> 
     <% @results.each do |pg_results| %> 
     <% if pg_results.searchable_type == "Album" %> 

      ####How can I link to the Albums index page 
      <%= link_to pg_results.searchable.name, %>  

     <% end %> 
    <% end %> 
    </div> 

<% end %> 

<% end %> 

MODELS

class Artist < ActiveRecord::Base 
    attr_accessible :name 

    has_many :albums 
    has_many :songs, :through => :albums 

    include PgSearch 
    multisearchable :against => [:name], 
    using: {tsearch: {dictionary: "english"}} 

end 

class Album < ActiveRecord::Base 
    attr_accessible :name, :artist_id 

    belongs_to :artist 
    has_many :songs 

    include PgSearch 
    multisearchable :against => [:name], 
    using: {tsearch: {dictionary: "english"}} 
    associated_against: {artist: [:artist_id, :name]} 

end 

ROUTES

Music::Application.routes.draw do 

resources :artist do 
    resources :albums 
end 

resources :results 

end 

SCHEMA

create_table "pg_search_documents", :force => true do |t| 
    t.text  "content" 
    t.integer "searchable_id" 
    t.string "searchable_type" 
    t.datetime "created_at",  :null => false 
    t.datetime "updated_at",  :null => false 
end 

回答

1

這是我的解決方案,鏈接到索引頁。有點混亂,如果有人有一個更容易的解決方案。請發表評論:)

<% @results.each do |pg_results| %> 
    <% if pg_results.searchable_type == "Artist" %> 
     <div class="span3"> 

     #### This is how I linked to the ALBUMS INDEX 
     <%= link_to pg_results.searchable.name, 
     {:action => "index", controller => "albums", :artist_id => => pg_results.searchable} 


     </div>  
    <% end %> 
<% end %>