zol*_*ter 5 ruby search ruby-on-rails highlighting sunspot
我想强调找到的单词文本,例如,如图所示这里.
据我所知,我必须遵循以下步骤:
1)在我的模型中,我必须在:stored => true我想要突出显示的字段中添加选项:
searchable do
text :title, :stored => true
text :description
end
Run Code Online (Sandbox Code Playgroud)
2)在我的控制器中,我必须声明我想要突出显示的字段:
def search
@search = Article.search do
keywords params[:search] do
highlight :title
end
end
end
Run Code Online (Sandbox Code Playgroud)
3)在视图中我不知道该怎么做,我试过这个:
- @search.each_hit_with_result do |hit, result|
%p= link_to raw(hit_title(hit)), article_path(result)
Run Code Online (Sandbox Code Playgroud)
这是做什么方法hit_title:
def hit_title(hit)
if highlight = hit.highlight(:title)
highlight.format { |word| "<font color='green'>#{word}</font>" }
else
h(hit.result.title)
end
end
Run Code Online (Sandbox Code Playgroud)
但它没有按预期工作,它总是突出显示标题的第一个单词,即使搜索到的单词位于其末尾.
有更简单的方法吗?
| 归档时间: |
|
| 查看次数: |
2474 次 |
| 最近记录: |