我正在尝试使用 Sunspot/Solr 搜索与其个人资料相关的所有用户名和内容(名为“bio”)。搜索框位于http://localhost:3000/users
。我显然没有做正确的事情,因为当我搜索一个名称时,我会返回到用户的第 1 页,其中包含完整列表,即没有执行任何搜索。我难住了。
当我跑步时rake sunspot:solr:reindex
undefined method `title' for #<User:0x007f9e9ea52e60>
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/activemodel-4.0.0/lib/active_model/attribute_methods.rb:436:in `method_missing'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/activerecord-4.0.0/lib/active_record/attribute_methods.rb:131:in `method_missing'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot-2.0.0/lib/sunspot/data_extractor.rb:17:in `value_for'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot-2.0.0/lib/sunspot/field_factory.rb:58:in `populate_document'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot-2.0.0/lib/sunspot/indexer.rb:100:in `block in prepare'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot-2.0.0/lib/sunspot/indexer.rb:99:in `each'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot-2.0.0/lib/sunspot/indexer.rb:99:in `prepare'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot-2.0.0/lib/sunspot/indexer.rb:26:in `block in add'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot-2.0.0/lib/sunspot/indexer.rb:26:in `map'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot-2.0.0/lib/sunspot/indexer.rb:26:in `add'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot-2.0.0/lib/sunspot/session.rb:91:in `index'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot-2.0.0/lib/sunspot/session_proxy/abstract_session_proxy.rb:11:in `index'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot-2.0.0/lib/sunspot/session_proxy/retry_5xx_session_proxy.rb:17:in `method_missing'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot-2.0.0/lib/sunspot/session_proxy/abstract_session_proxy.rb:11:in `index'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot-2.0.0/lib/sunspot.rb:184:in `index'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot_rails-2.0.0/lib/sunspot/rails/searchable.rb:261:in `block (2 levels) in solr_index'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot_rails-2.0.0/lib/sunspot/rails/searchable.rb:365:in `solr_benchmark'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot_rails-2.0.0/lib/sunspot/rails/searchable.rb:260:in `block in solr_index'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/activerecord-4.0.0/lib/active_record/relation/batches.rb:75:in `find_in_batches'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/activerecord-deprecated_finders-1.0.3/lib/active_record/deprecated_finders/relation.rb:70:in `find_in_batches'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/activerecord-deprecated_finders-1.0.3/lib/active_record/deprecated_finders/relation.rb:68:in `find_in_batches'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/activerecord-4.0.0/lib/active_record/querying.rb:8:in `find_in_batches'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot_rails-2.0.0/lib/sunspot/rails/searchable.rb:259:in `solr_index'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot_rails-2.0.0/lib/sunspot/rails/searchable.rb:203:in `solr_reindex'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot_rails-2.0.0/lib/sunspot/rails/tasks.rb:64:in `block (3 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot-2.0.0/lib/sunspot/class_set.rb:16:in `each'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot-2.0.0/lib/sunspot/class_set.rb:16:in `each'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/sunspot_rails-2.0.0/lib/sunspot/rails/tasks.rb:63:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/bin/ruby_noexec_wrapper:14:in `eval'
/usr/local/rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => sunspot:solr:reindex => sunspot:reindex
(See full trace by running task with --trace)
模型/bio.rb
class User < ActiveRecord::Base
searchable do
text :title, :name, :content, :body
end
end
bio_controllers.rb(这是一个独立于 user_controller.rb 的文件)
class BioController < ApplicationController
def index
@search = users.search do
fulltext params[:search]
end
@users = @search.results
end
index.html.erb
<% provide(:title, 'All users') %>
<%= form_tag users_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
<h1>All users</h1>
<%= will_paginate %>
<ul class="users">
<%= render @users %>
</ul>
<%= will_paginate %>
数据库/迁移/...
class AddBioToUserProfile < ActiveRecord::Migration
def change
add_column :users, :bio, :string
end
end
用户.rb
class User < ActiveRecord::Base
searchable do
text :name, :bio, :users
end
before_save { self.email = email.downcase }
before_create :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
#VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
#format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }
def User.new_remember_token
SecureRandom.urlsafe_base64
end
def User.encrypt(token)
Digest::SHA1.hexdigest(token.to_s)
end
private
def create_remember_token
self.remember_token = User.encrypt(User.new_remember_token)
end
end