我正在尝试实现Thinking Sphinx 2.0.10,据我所知,它与Rails 3兼容。我对使用RubyonRails编程非常陌生。我已经阅读了很多关于StackOverflow的文章,但找不到解决问题的方法。
我在Gemfile中使用安装了没有:require参数的gem。
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代码。注释掉的行是正在工作的原始代码。我在应用程序控制器中有一个默认的will_page,每页15条记录。
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调试这个问题。在过去的几天里,我在这个网站和其他许多网站上搜索了很多页面,但没有一个涉及这个问题。如有任何帮助,我们将不胜感激。
是否初始化了sphinx数据库?
通常你只需要做一些类似的事情:
rake ts:rebuild
这将自动为您运行rake ts:conf
并重新生成索引。
您也可以使用rake ts:in
来更新索引。
测试这是否有效的一个简单方法是运行rails控制台(rails c
)并尝试手动搜索用户(User.search
)。
如果您得到任何结果,索引是可用的,接下来您可以对视图/控制器进行故障排除;)