首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >‘’twitter‘

‘’twitter‘
EN

Stack Overflow用户
提问于 2014-12-24 21:30:36
回答 2查看 311关注 0票数 2

我正在尝试将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

代码语言:javascript
复制
<div id="search-wrapper">
  <input class="typeahead" type="text" placeholder="Search">
</div>

app/assets/javascripts/search.js.coffee

代码语言:javascript
复制
$(document).on 'ready page:load', -&gt;
  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

代码语言:javascript
复制
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
复制
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>

如果我删除它,错误就会消失,但这显然不是一个更好的解决方案。

第一个错误:

代码语言:javascript
复制
SyntaxError: [stdin]: unexpected &
  (in home/website/app/assets/javascripts/search.js.coffee)

然后,如果从->中删除search.js.coffee第一行中的"&“,就会得到以下错误:

第二错误:

代码语言:javascript
复制
SyntaxError: [stdin]:2:1: unexpected indentation
  (in home/website/app/assets/javascripts/search.js.coffee)

我玩了一些凹痕,但这个错误我无法消失。

更新:--我按照NickM的建议修改了search.js.coffee代码,但现在我得到了一个不同的错误。

更新的search.js.coffee

代码语言:javascript
复制
$(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
    }
  )

新误差

代码语言:javascript
复制
SyntaxError: [stdin]:6:11: unexpected (
  (in /home/website/app/assets/javascripts/search.js.coffee)
EN

回答 2

Stack Overflow用户

发布于 2014-12-25 00:21:29

试着把这个粘贴到你的咖啡记录文件中。看上去里面有几个有趣的人物和一个无与伦比的出类拔萃者:

代码语言:javascript
复制
$(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文件的内容粘贴在右侧窗格中,它将显示您是否有语法错误,或者是否弹出有趣的字符。我一直在用这个。希望能帮上忙。

票数 1
EN

Stack Overflow用户

发布于 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

谷歌搜索将产生更多的结果。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27642322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档