如何将PGSeaerch结果链接到嵌套资源中的索引页? [英] How to Link PGSeaerch results to Index Page in Nested Resources?
本文介绍了如何将PGSeaerch结果链接到嵌套资源中的索引页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我终于想出了如何实现pg_search的多重搜索功能。
但是我在制作一个显示链接的可用搜索页面时遇到了麻烦返回到适当的嵌套页面。
But I'm having trouble making a usable search page that displays links back to the appropriate nested pages.
如何传递正确的ID,以便可以链接到嵌套视图。
How can I pass the proper id so I could link to the nested views.
所以类似于此link_to
myapp.com/artists/1/albums
到目前为止,我收到此错误(我似乎无法通过艺术家ID)
So far I get this error(i cant seem to pass the artist id)
No route matches {:controller=>"albums", :artists=>nil}
Rails新手请帮助:)
New to Rails Please help :)
控制器
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
视图
<% 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 %>
模型
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
路线
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
推荐答案
这是我的解决方案链接到索引页面。如果有人有更简单的解决方案,那有点混乱。请发表评论:)
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 %>
这篇关于如何将PGSeaerch结果链接到嵌套资源中的索引页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文