gpt4 book ai didi

ruby-on-rails - 如何将 PGSeaerch 结果链接到嵌套资源中的索引页?

转载 作者:行者123 更新时间:2023-11-29 13:33:01 24 4
gpt4 key购买 nike

我终于想出了如何实现 pg_search 的多搜索功能。

但是我在制作一个可用的搜索页面时遇到了问题,该搜索页面显示返回到适当嵌套页面的链接。

如何传递正确的 ID,以便链接到嵌套 View 。

所以像这个 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

观点

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

架构

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

最佳答案

这是我链接到索引页的解决方案。有点乱,如果有人有更简单的解决方案。请发表评论:)

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

关于ruby-on-rails - 如何将 PGSeaerch 结果链接到嵌套资源中的索引页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19398866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com