0

我正試圖在項目上完成一個自動完成搜索欄(用於標題和照片描述)。我在這個項目上的合作伙伴已經實施的照片控制器下面的搜索行動typeahead.js和rails搜索,遠程選項不顯示結果

def search_results 
    tag = params[:q].values.first 
    puts(tag) 
    @photos= @q.result(distinct: true).to_a 
    @photos += Photo.tagged_with(tag).flatten 
    @photos.uniq! 
    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @photos } 
    end 
end 

//And in my application.js 
$(document).ready(function(){ 
$('#typeaheadBar').typeahead([ 
    { 
    name: 'mysearch', 
    displayKey: 'title', 
    remote: '/search?utf8=%E2%9C%93&q%5Btitle_or_caption_cont%5D=%QUERY' 
    } 
]); 

}); 

我看不出有什麼不對,但我的教練說我接近,我知道的預輸入,返回的東西,但我不不知道如何引用它。有人能指出我的方向是正確還是指出我做錯了什麼?謝謝。

回答

0

typeahead在數據集定義中沒有remote選項。查看可用選項here。您應該使用Bloodhound建議引擎從遠程獲取您的數據。你可以找到例子here。在你的情況下:

var engine = new Bloodhound({ 
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('title'), 
    queryTokenizer: Bloodhound.tokenizers.whitespace, 
    remote: '../data/films/queries/%QUERY.json' 
}); 

engine.initialize(); 

// The first argument is an option object, the additional arguments are 
// the definitions of your datasets. 
$('#typeaheadBar').typeahead(null, { 
    name: 'mysearch', 
    displayKey: 'title', 
    source: engine.ttAdapter() 
}); 
+0

謝謝!我通過改變我的控制器來排除它,只是把標題吐出來,但實施你的建議,以便我可以搜索這兩個。感謝您指出typeahead沒有遠程選項。 – Sri