How to Link PGSeaerch results to Index Page in Nested Resources?

ぐ巨炮叔叔 提交于 2019-12-30 11:52:37

问题


I finally figured out how to implement pg_search's multisearch feature.

But I'm having trouble making a usable search page that displays links back to the appropriate nested pages.

How can I pass the proper id so I could link to the nested views.

So something like this link_to

myapp.com/artists/1/albums

So far I get this error(i cant seem to pass the artist id)

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

New to Rails Please help :)

CONTROLLERS

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:


This was my solution to linking to the index page. Kind of messy, if someone has an easier solution. please leave a comment :)

 <% @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 %>


来源:https://stackoverflow.com/questions/19398866/how-to-link-pgseaerch-results-to-index-page-in-nested-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!