2013-04-07 45 views
0

我有一個顯示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 

,顯示在搜索結果中的插圖列表中的部分。該應用程序調用上的主要展示方法堅持即使我給你在的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 %> 

應該表現出圖文並茂的網格中的頁面,標題爲searched_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

這是不好的,但它是一個解決方案:

在搜索控制器:

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 
0

那麼這將有很大的幫助,如果你是更具體的瞭解您所使用的搜索寶石,但是讓我們假設你的使用meta_searchmeta_search,我相信其他一些搜索寶石,返回類Search的對象,並且您需要調用.results方法來獲取搜索結果。

它就像你如何不能撥打@users = User,你必須致電@users = User.all。在這種情況下,它@listings = Listing.search(params[:search]).results.all

相關問題