我的应用上有自动完成功能。自动完成工作正常,打字给我正确的结果。但是,除了简单地填充搜索框之外,我希望这些结果是可点击的链接。
产品总监:
def index
@products = Product.where("title like ?", "%#{params[:title]}%")
respond_to do |format|
format .js
format.html # index.html.erb
format.json { render json: @products.map(&:id) }
end
end
咖啡脚本:
jQuery ->
$('#search').autocomplete
source: $('#search').data('autocomplete-source')
查看:
<%= text_field_tag :search, params[:search], data: {autocomplete_source: products_path(id: 'product.id') } %>
所以我猜我需要在JSON中传递id,然后让Jquery在我链接时显示id,但我不知道如何。感谢。