0

我在 index.html.haml 中显示我所有的饮料时遇到问题。在观看了 Ryan Bates 之后,我最近开始使用 Thinking sphinx 进行搜索(thinking sphinx railscast。作为转向 sphinx 的一部分,我改为@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

饮料指数.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
4

1 回答 1

1

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

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

rake ts:rebuild
于 2013-02-12T21:49:43.613 回答