2012-02-22 90 views
0

我試圖實現思維獅身人面像2.0.10,從我的理解是與Rails 3兼容。我是使用Ruby on Rails進行編程的新手。我在StackOverflow上閱讀了很多文章,但找不到解決我的問題的方法。使用思維獅身人面像與Rails的問題3.2.1

我安裝了gem,但沒有在我的Gemfile中使用require參數。

gem 'thinking-sphinx', '2.0.10' 

我有一個工作的Rails應用程序,它顯示了我想要添加搜索的列表。

這是我在我的模型文件中定義索引的代碼。

define_index do 
    indexes :email, :sortable => true 
    indexes :name, :sortable => true 
    indexes microposts.content, :as => :micropost_content 
end 

這是我的控制器文件中的Rails代碼。註釋掉的行是正在工作的原始代碼。我在應用程序控制器中爲每頁15個記錄設置了will_paginate的默認值。

def index 
    @users = User.search params[:search], :per_page => 15 
    # @users = User.paginate(page: params[:page]) 
end 

這是我添加到我的索引頁的搜索框。

<p> 
    <%= text_field_tag :search, params[:search] %> 
    <%= submit_tag "Search", :name => nil %> 
</p> 

這是我的rSpec代碼。這是我在嘗試實施Thinking Sphinx之前使用的原始代碼。

describe "index" do 
    let(:user) { FactoryGirl.create(:user) } 
    before(:each) do 
    sign_in user 
    visit users_path 
    end 
    it { should have_selector('title', text: 'All users') } 

    describe "pagination" do 
    before(:all) { 15.times { FactoryGirl.create(:user) } } 
    after(:all) { User.delete_all } 
    let(:first_page) { User.paginate(page: 1) } 
    let(:second_page) { User.paginate(page: 2) } 
    it { should have_link('Next') } 
    it { should have_link('2') } 
    it { should_not have_link('delete') } 
    it "should list each user" do 
     User.all[0..2].each do |user| 
     page.should have_selector('li', text: user.name) 
     end 
    end 

    it "should list the first page of users" do 
     first_page.each do |user| 
     page.should have_selector('li', text: user.name) 
     end 
    end 
    it "should not list the second page of users" do 
     second_page.each do |user| 
     page.should_not have_selector('li', text: user.name) 
     end 
    end 
    describe "as an admin user" do 
     let(:admin) { FactoryGirl.create(:admin) } 
     before do 
     sign_in admin 
     visit users_path 
     end 
     it { should have_link('delete', href: user_path(User.first)) } 
     it "should be able to delete another user" do 
     expect { click_link('delete') }.to change(User, :count).by(-1) 
     end 
     it { should_not have_link('delete', href: user_path(admin)) } 
    end 

    end 
end 

當我跑我的RSPEC測試,我得到以下錯誤:

Failure/Error: visit users_path 
    ActionView::Template::Error: 
     getaddrinfo: nodename nor servname provided, or not known 

該頁面顯示就好了。當我在搜索框中輸入文本並單擊按鈕時,沒有任何事情發生,這對我來說並不令人意外。

我能夠在終端上運行獅身人面像。搜索工作正常。我只是不知道用Thinking Sphinx來調試這個問題。我在這個網站上搜索了很多頁面,在過去的幾天裏也搜索了很多其他頁面,但是他們都沒有處理這個問題。任何幫助,將不勝感激。

回答

0

初始化sphinx數據庫嗎?

通常你需要做的僅僅是這樣的:

rake ts:rebuild 

這應該自動運行rake ts:conf爲您和重建索引。您可以使用rake ts:in更新索引。

測試此功能的簡單方法是運行導軌控制檯(rails c)並嘗試手動搜索您的用戶(User.search)。

如果你得到任何結果,該指標可用,你可以下一個解決您的意見/控制器;)

+0

鮃,我不得不退出的,因爲這樣的生活工作:)我只是做了你的建議。思維獅身人面像找到了很好的記錄。我需要弄清楚如何更新視圖/控制器。謝謝您的幫助。 – 2012-04-25 19:09:33

+0

由於我使用PostgreSQL來處理我的Rails應用程序,我決定爲我的文本搜索安裝pg_search gem。一切都在引擎蓋下。它對文本搜索非常有效。 – 2013-11-13 22:41:27