我正在尝试将twitter-typeahead gem添加到我的应用程序中,并遵循本教程:http://blog.sed.hu/2014/07/11/ruby-on-rails-search-autocomplete-with-sunspot-solr-and-twitter-typeahead-js-part-2/。我犯了几个不同的错误。是否有人使用了“提前打字机”,却发现教程中有什么明显的错误?还是我做错什么了?
,这是我所做的:
application.html.erb
<div id="search-wrapper">
<input class="typeahead" type="text" placeholder="Search">
</div>app/assets/javascripts/search.js.coffee
$(document).on 'ready page:load', ->
users = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/users/index?q=%QUERY'
}
})
users.initialize()
$('input.typeahead').typeahead(
{
hint: true,
highlight: true,
minLength: 1
},
{
name: 'things',
displayKey: 'title',
source: User.all
})controllers/users_controller.rb
def index
if params[:search]
@user = User.search(params[:search]).order("created_at DESC")
respond_to do |format|
format.json do
results = @user.results.map do |user|
{ title: user.name }
end
render json: results
end
else
@user = User.order("created_at DESC")
end
end错误:
这两个错误都指向我脑海中的这一行:
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>如果我删除它,错误就会消失,但这显然不是一个更好的解决方案。
第一个错误:
SyntaxError: [stdin]: unexpected &
(in home/website/app/assets/javascripts/search.js.coffee)然后,如果从->中删除search.js.coffee第一行中的"&“,就会得到以下错误:
第二错误:
SyntaxError: [stdin]:2:1: unexpected indentation
(in home/website/app/assets/javascripts/search.js.coffee)我玩了一些凹痕,但这个错误我无法消失。
更新:--我按照NickM的建议修改了search.js.coffee代码,但现在我得到了一个不同的错误。
更新的search.js.coffee
$(document).on 'ready page:load',
users = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/users/index?q=%QUERY'
}
})
users.initialize()
$('input.typeahead').typeahead(
{
hint: true,
highlight: true,
minLength: 1
},
{
name: 'things',
displayKey: 'title',
source: User.all
}
)新误差
SyntaxError: [stdin]:6:11: unexpected (
(in /home/website/app/assets/javascripts/search.js.coffee)发布于 2014-12-25 00:21:29
试着把这个粘贴到你的咖啡记录文件中。看上去里面有几个有趣的人物和一个无与伦比的出类拔萃者:
$(document).on 'ready page:load', ->
users = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/users/index?q=%QUERY'
}
})
users.initialize()
$('input.typeahead').typeahead(
{
hint: true,
highlight: true,
minLength: 1
},
{
name: 'things',
displayKey: 'title',
source: User.all
}
)另外,您可能会发现js2coffee工具很有用,可以找到这里。
如果您将Coffeescript文件的内容粘贴在右侧窗格中,它将显示您是否有语法错误,或者是否弹出有趣的字符。我一直在用这个。希望能帮上忙。
发布于 2015-01-14 00:30:31
有很多其他伟大的教程在那里。也许你会有更多的运气,其中一些:
http://www.arungudelli.com/jquery/simple-jquery-autocomplete-search-tutorial/ http://blattchat.com/2013/01/09/bootstrap-typeahead/ http://www.tutorialrepublic.com/twitter-bootstrap-tutorial/bootstrap-typeahead.php https://twitter.github.io/typeahead.js/examples/ http://www.bootply.com/86571
谷歌搜索将产生更多的结果。
https://stackoverflow.com/questions/27642322
复制相似问题