2013-04-09 118 views
0

我遵循這裏提到的說明Rails 3: sunspot solr : adding search ability for every page太陽黑子搜索只能從一個頁面中搜索?

但是,它似乎並沒有爲我工作。我試圖實現一個類似於搜索功能的twitter,其中搜索欄位於導航欄的頂部,並且可以搜索應用程序中的任何頁面,並將它們帶到搜索結果頁面。

當我通過搜索查詢瀏覽器,而在帖子的網頁(瀏覽量/職位/ index.html.erb)

http://localhost:3000/post?utf8=%E2%9C%93&search=test&commit=Search 

它返回所需的搜索結果。但是,當我在應用程序的其他頁面中,並嘗試在地址欄中搜索它的表單url時,就像下面提到的那樣,並且永遠不會通過views/layouts /中聲明的頂部導航欄中的搜索框進行搜索。 application.html.erb頁面。

http://localhost:3000/users/1/following?utf8=%E2%9C%93&search=test&commit=Search 

http://localhost:3000/posts/1/favorite?utf8=%E2%9C%93&search=test&commit=Search 

下面是詳細信息: -

我的模型 post.rb

searchable do 
    text :title, :tag_list 
    end 

我的控制器 - posts_controller.rb

def index 


     if params[:search] 

     @search = Post.search do 
      fulltext params[:search] 
      paginate :page => params[:page], :per_page => 5   
     end 

     @posts = @search.results 
     else 

       @posts = @posts.plusminus_tally({:order => "vote_count ASC"}).page(params[:page]).per(6) 

     end 

     respond_to do |format| 
     format.html 
     format.json { render json: @posts} 
     end 
    end 

查看 - 佈局/ application.rb中

<%= form_tag posts_path, :method => :get do %> 
        <div class="input-append"> 
         <%= text_field_tag :search, params[:search] , :class => "span2 search-query", :style => 'width:200px' %> 
         <%= submit_tag "Search", :post => nil , :class => "btn" %> 
        </div> 
        <% end %> 
+0

你可能想看看這個railscasts視頻 - http://railscasts.com/episodes/278-search-with-sunspot – David 2013-04-10 03:35:47

+0

謝謝大衛。我之前看過這個視頻,並沒有提到在整個網站上進行搜索的方法。 – ArkoD 2013-04-10 08:51:37

回答

0

我終於想通了這一點。這需要在當前頁面的每一個控制器上被稱爲重定向搜索發佈到其搜索結果頁面(在這種情況下帖子#指數)

收藏控制器 & 用戶控制器

if params[:search] 
      search_param = CGI::escapeHTML(params[:search])  

      redirect_to ("/posts?search=#{search_param}&commit=Search") 

      return 
     end