我正在尝试为我的搜索栏设置自动填充功能,这样当您输入自动填充功能时,会向您提供类似名称的帖子,这样您就不必继续输入内容了。我在控制台中没有输出任何输出,或者在Chrome控制台中没有任何错误。
我已经使用了几个教程来尝试设置它。但这是我最常看的那个人:
A link to my project's github in case you guys need more information
他们都有一些类似的方法来设置它,所以我认为没问题。但我仍然无法让自动完成工作。
当我跑步时:
bundle exec rake routes
我得到了这个,这使得它看起来没有自动完成路径,但它应该有一个,因为我在listing_controller.rb中创建了自动完成方法,不是吗? :
like_listing_comment PUT /listings/:listing_id/comments/:id/like(.:format) comments#upvote
unlike_listing_comment PUT /listings/:listing_id/comments/:id/unlike(.:format) comments#downvote
listing_comments GET /listings/:listing_id/comments(.:format) comments#index
POST /listings/:listing_id/comments(.:format) comments#create
new_listing_comment GET /listings/:listing_id/comments/new(.:format) comments#new
edit_listing_comment GET /listings/:listing_id/comments/:id/edit(.:format) comments#edit
listing_comment GET /listings/:listing_id/comments/:id(.:format) comments#show
PATCH /listings/:listing_id/comments/:id(.:format) comments#update
PUT /listings/:listing_id/comments/:id(.:format) comments#update
DELETE /listings/:listing_id/comments/:id(.:format) comments#destroy
like_listing PUT /listings/:id/like(.:format) listings#upvote
unlike_listing PUT /listings/:id/unlike(.:format) listings#downvote
search_listings GET /listings/search(.:format) listings#search
autocomplete_listings GET /listings/autocomplete(.:format) listings#autocomplete
listings GET /listings(.:format) listings#index
POST /listings(.:format) listings#create
new_listing GET /listings/new(.:format) listings#new
edit_listing GET /listings/:id/edit(.:format) listings#edit
listing GET /listings/:id(.:format) listings#show
PATCH /listings/:id(.:format) listings#update
PUT /listings/:id(.:format) listings#update
DELETE /listings/:id(.:format) listings#destroy
的routes.rb
resources :listings do
resources :comments do
member do
put "like" => "comments#upvote"
put "unlike" => "comments#downvote"
end
end
member do
put "like" => "listings#upvote"
put "unlike" => "listings#downvote"
end
collection do
get 'search'
get :autocomplete
end
端
我的listing_controller.rb中的自动完成方法,您会注意到我将查询命名命名为查询,而不是像示例中那样查询,因为我的搜索栏名为search,它的查询方式就是这样命名的
def search
if params[:search]
@listings = Listing.search(params[:search])
else
@listings = Listing.all
end
end
def autocomplete
render json: Listing.search(params[:search], autocomplete: false, limit: 10).map do |listing|
{ title: listing.title, value: listing.id }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_listing
@listing = Listing.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def listing_params
params.require(:listing).permit(:name, :code, :language, :private)
end
我不确定为什么它不起作用,有人可以看看吗?我觉得我错过了一些可能是我一直在使用的教程遗漏的东西。
编辑:我在帖子中发现有人试图访问他们的自动完成链接,并且在他们的网页上返回JSON。然而,我的不是。当我尝试访问时:http://localhost:3000/listings/autocomplete?query=%Untitl我应该得到一个包含大量内容的json文本,因为我有很多名为Untitled的列表。但是我的控制台显示了一个很长的错误:
2016-04-16 20:08:22 -0500:HTTP解析错误,格式错误的请求():#
2016-04-16 20:08:22 -0500:ENV:{" rack.version" => [1,3]," rack.errors" => ;#>," rack.multithread" => true," rack.multiprocess" => false," rack.run_once" => false, " SCRIPT_NAME" =>""," QUERY_STRING" =>""," SERVER_PROTOCOL" => ;" HTTP / 1.1"," SERVER_SOFTWARE" =>" 3.1.0"," GATEWAY_INTERFACE" =>" CGI / 1.2"," REQUEST_METHOD" =>" GET"," REQUEST_PATH" =>" / listings / autocomplete" }
listings.js,我已将单词书籍更改为列表,并尝试更改行:url:' ../ listings / autocomplete?query =%QUERY' to url:&# 39; ../ listing / autocomplete?search =%SEARCH',' ../ listings / autocomplete?search =%QUERY',' ../ listings / autocomplete?query =%搜索'这些都没有。
var ready;
ready = function() {
var engine = new Bloodhound({
datumTokenizer: function(d) {
console.log(d);
return Bloodhound.tokenizers.whitespace(d.title);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '../listings/autocomplete?query=%QUERY'
}
});
var promise = engine.initialize();
promise
.done(function() { console.log('success })
.fail(function() { console.log('err });
$('.typeahead').typeahead(null, {
name: 'engine',
displayKey: 'title',
source: engine.ttAdapter()
});
}
$(document).ready(ready);
$(document).on('page:load', ready);
编辑: 我认为javascript可能存在问题,在Atom Text Editor中它强调了这一部分的成功:
promise
.done(function() { console.log('success })
.fail(function() { console.log('err });
我已尝试将其更改为:
promise
.done(function() { console.log('success' });
.fail(function() { console.log('err' });
但是问题仍然存在,我不太了解javascript,但这条线只能打印成功或错误到控制台,对吧?没有别的因此不应该影响任何事情吗?
答案 0 :(得分:1)
网址指定不应该有2个点。
url: '../listings/autocomplete?query=%QUERY'
应该是
url: '/listings/autocomplete?query=%QUERY'
示例应用程序与教程一起: https://github.com/trh/rails-typeahead-sample-app