我有一个页面显示3个搜索结果列表:插图,小说和标签。我想允许用户单击每个列表的链接以查看缩略图网格。我不能为我的生活弄清楚如何将搜索结果对象传递给新的页面。我只是得到插图ID的字符串。
main_controller的搜索方法,以及我要对show_illustrations做的事情:
def search
@search_criteria = params[:search]
@novels = Novel.search(params[:search])
@illustrations = Illustration.search(params[:search])
@tags = Tag.search(params[:search])
respond_to do |format|
format.html #search_results.html.erb
end
end
def show_illustrations
@illustrations = params[:illustrations]
end
显示搜索结果中插图列表的部分。应用程序坚持调用main的show方法,即使我将link_to语句中的操作分配给'show_illustrations':
<% if @illustrations.size() > 0 %>
<div class="row-fluid">
<div class="span8">
<h3>Illustrations</h3>
</div>
<div class="span4">
<%= link_to "View", {:controller => "main", :action => "show_illustrations", :illustrations => @illustrations}, :class => "btn btn-mini"%>
</div></div>
<div class="search_results well-search-results">
<table class="table table-striped table-condensed">
<% @illustrations.each do |illustration| %>
<tr>
<td><%= illustration.name %></td>
<td><%= link_to "Show", illustration, :class => "btn btn-primary btn-mini", :style => "float:right;" %></td>
</tr>
<% end %>
</table>
</div>
<% else %>
<h3>No Matching Illustrations</h3>
<% end %>
应显示插图网格的页面,标题为searching_illustrations.html.erb:
<div class="container">
<div class="well padding:0px;">
<% @illustrations.each_slice(4) do |set| %>
<div class="row-fluid">
<% set.each do |illustration| %>
<div class="span3">
<div style="text-align:center"><%= link_to image_tag(illustration.image_thumbnail_url), illustration %>
<br />
<%= illustration.name %><br />
<%= link_to illustration.novel.name, illustration.novel %> -
<%= illustration.novel.edition %>
<% if ! illustration.tags.empty? %>
<br />
<% illustration.tags.each do |tag| %>
<%= tag %>
<% end %>
<% end %>
</div>
<br />
</div>
<% end %>
</div>
<br \>
<% end %>
</div>
</div>
routes.rb中:
match 'main/show_illustrations' => 'main#show_illustrations', as: :show_illustrations
答案 0 :(得分:0)
如果您更具体地了解您正在使用哪种搜索gem,那么它会有很大帮助,但让我们假设您使用meta_search
。 meta_search
,我相信其他一些搜索宝石,会返回类Search
的对象,您需要调用.results
方法来获取搜索结果。
就像你如何打电话@users = User
一样,你必须致电@users = User.all
。在这种情况下,它是@listings = Listing.search(params[:search]).results.all
答案 1 :(得分:0)
这很糟糕,但这是一个解决方案:
在搜索控制器中:
def show_illustrations
@arr = params[:illustrations]
@illustrations = Array.new
@arr.each do |id|
illustration = Illustration.find(id)
@illustrations << illustration
end
@illustrations = Kaminari.paginate_array(@illustrations).page(params[:page]).per(20)
end