列出索引中的搜索

时间:2013-02-12 21:04:14

标签: ruby-on-rails ruby-on-rails-3 thinking-sphinx

我在index.html.haml中显示出所有饮料的问题。我最近开始使用思考狮身人面像进行搜索后观看了Ryan Bates'(thinking sphinx railscast。作为迈向狮身人面像的一部分,我将@drinks = Drink.all更改为@drinks = Drink.search(params[:search]),现在它没有显示饮料我的索引页上的名字

饮料模型

class Drink < ActiveRecord::Base

  attr_accessible :name, :detail, :recipe_steps_attributes

  has_many :recipe_steps, :dependent => :destroy
  has_many :ingredients, through: :recipe_steps
  has_one :glass

  validates_uniqueness_of :name, case_sensitive: false

  accepts_nested_attributes_for :recipe_steps, :reject_if => lambda { |a| a[:amount].blank? }, :allow_destroy => true

  define_index do
    indexes :name
    indexes ingredients.name as: :ingredient_name
  end
end

饮料控制器指数

def index
    @drinks = Drink.search(params[:search])
    if current_user
      @cabinet = Cabinet.find(current_user.id)
    end
  end

喝index.haml

= form_tag drinks_path, method: :get do
  .field
    = text_field_tag :search, params[:search]
    = submit_tag "Search", name: nil

- @drinks.each do |drink|
  = drink.name

1 个答案:

答案 0 :(得分:1)

我能够回答我自己的问题。问题是我在向数据库添加记录后没有重新编入索引。因此,当我试图将它们打印出来时,它们没有显示出来。

正如TomL建议的那样,解决这个问题的最佳方法是定期运行一个cron作业

rake ts:rebuild