Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 让typeahead使用rails搜索_Ruby On Rails_Ruby On Rails 4_Search_Autocomplete_Typeahead.js - Fatal编程技术网

Ruby on rails 让typeahead使用rails搜索

Ruby on rails 让typeahead使用rails搜索,ruby-on-rails,ruby-on-rails-4,search,autocomplete,typeahead.js,Ruby On Rails,Ruby On Rails 4,Search,Autocomplete,Typeahead.js,我让typeahead和search在rails 4应用程序中使用typeahead v0.10.5,但自从更新到v0.11.1后,它就坏了。我最初是从这个网站上的各种答案中获得代码的,但我并不真正了解到底发生了什么。我现在大部分时间都在工作,但下拉列表中填充了我模型中所有属性的文本,而不仅仅是我想要的名称。我想确保我做的事情是正确的,并希望能更好地了解正在发生的事情 item.rb def self.search(search) if search where(['lo

我让typeahead和search在rails 4应用程序中使用typeahead v0.10.5,但自从更新到v0.11.1后,它就坏了。我最初是从这个网站上的各种答案中获得代码的,但我并不真正了解到底发生了什么。我现在大部分时间都在工作,但下拉列表中填充了我模型中所有属性的文本,而不仅仅是我想要的名称。我想确保我做的事情是正确的,并希望能更好地了解正在发生的事情

item.rb

  def self.search(search)
    if search
      where(['lower(name) LIKE ?', "%#{search}%"])
    else
      Item.all
    end
  end
  def index
    @items= Item.search(params[:query])
  end

  def typeahead
    render json: Item.where('name ilike ?', "%#{params[:query]}%")
  end
<div role="search">
  <%= form_tag items_path, class: 'navbar-form', method: :get do %>
    <div class="form-group">
      <%= text_field_tag :query, params[:query], id: 'typeahead', class: 'search-input form-control',
                                        placeholder: 'Search items' %>
    </div>
    <span class="search-icon">
      <%= button_tag "<i class='fa-navbar fa fa-search'></i>".html_safe, name: nil, class: 'search-button' %>
    </span>
  <% end %>
</div>
.
.
.
<script>
$(document).ready(function(){
    var bloodhound = new Bloodhound({
      datumTokenizer: function (d) {
        return Bloodhound.tokenizers.whitespace(d.value);
        },
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      limit: 10,  
      remote: {url: '/typeahead/%QUERY',
               wildcard: '%QUERY'}  
    });
    bloodhound.initialize();

    $('#typeahead').typeahead(null, {
      name: 'name',
      source: bloodhound.ttAdapter()
    });

    $('#typeahead').bind('typeahead:selected', function(event, datum, name) {
      window.location.href = '/items/' + datum.id;
    });
  });
</script>
get 'typeahead/:query', to: 'items#typeahead'
items\u controller.rb

  def self.search(search)
    if search
      where(['lower(name) LIKE ?', "%#{search}%"])
    else
      Item.all
    end
  end
  def index
    @items= Item.search(params[:query])
  end

  def typeahead
    render json: Item.where('name ilike ?', "%#{params[:query]}%")
  end
<div role="search">
  <%= form_tag items_path, class: 'navbar-form', method: :get do %>
    <div class="form-group">
      <%= text_field_tag :query, params[:query], id: 'typeahead', class: 'search-input form-control',
                                        placeholder: 'Search items' %>
    </div>
    <span class="search-icon">
      <%= button_tag "<i class='fa-navbar fa fa-search'></i>".html_safe, name: nil, class: 'search-button' %>
    </span>
  <% end %>
</div>
.
.
.
<script>
$(document).ready(function(){
    var bloodhound = new Bloodhound({
      datumTokenizer: function (d) {
        return Bloodhound.tokenizers.whitespace(d.value);
        },
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      limit: 10,  
      remote: {url: '/typeahead/%QUERY',
               wildcard: '%QUERY'}  
    });
    bloodhound.initialize();

    $('#typeahead').typeahead(null, {
      name: 'name',
      source: bloodhound.ttAdapter()
    });

    $('#typeahead').bind('typeahead:selected', function(event, datum, name) {
      window.location.href = '/items/' + datum.id;
    });
  });
</script>
get 'typeahead/:query', to: 'items#typeahead'
\u header.html.erb

  def self.search(search)
    if search
      where(['lower(name) LIKE ?', "%#{search}%"])
    else
      Item.all
    end
  end
  def index
    @items= Item.search(params[:query])
  end

  def typeahead
    render json: Item.where('name ilike ?', "%#{params[:query]}%")
  end
<div role="search">
  <%= form_tag items_path, class: 'navbar-form', method: :get do %>
    <div class="form-group">
      <%= text_field_tag :query, params[:query], id: 'typeahead', class: 'search-input form-control',
                                        placeholder: 'Search items' %>
    </div>
    <span class="search-icon">
      <%= button_tag "<i class='fa-navbar fa fa-search'></i>".html_safe, name: nil, class: 'search-button' %>
    </span>
  <% end %>
</div>
.
.
.
<script>
$(document).ready(function(){
    var bloodhound = new Bloodhound({
      datumTokenizer: function (d) {
        return Bloodhound.tokenizers.whitespace(d.value);
        },
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      limit: 10,  
      remote: {url: '/typeahead/%QUERY',
               wildcard: '%QUERY'}  
    });
    bloodhound.initialize();

    $('#typeahead').typeahead(null, {
      name: 'name',
      source: bloodhound.ttAdapter()
    });

    $('#typeahead').bind('typeahead:selected', function(event, datum, name) {
      window.location.href = '/items/' + datum.id;
    });
  });
</script>
get 'typeahead/:query', to: 'items#typeahead'
  • 我的数据来源于基于用户输入的模型。在我的示例中,有预取的地方吗

  • 在遥控器中,我将url设置为
    “/typeahead/%QUERY”
    。在其他示例中,我看到了类似于
    “/typeahead?q=%QUERY”
    。有什么区别

  • 猎犬中的准备和通配符选项是什么?我已经阅读了api说明,但仍然不理解


  • “建议”下拉列表正确显示的问题是,我缺少了作为typeahead选项的
    displayKey:“name”
    。添加此项修复了我的问题

    $('#typeahead').typeahead(null, {
      name: 'name',
      displayKey: 'name',
      source: bloodhound.ttAdapter()
    });
    

    “建议”下拉列表正确显示的问题是,我缺少了作为typeahead选项的
    displayKey:“name”
    。添加此项修复了我的问题

    $('#typeahead').typeahead(null, {
      name: 'name',
      displayKey: 'name',
      source: bloodhound.ttAdapter()
    });
    

    您重新启动了rails服务器吗?@duhaime是的,我的问题是我缺少typeaheadah下的
    显示
    选项,很好!如果你找到了一个解决方案,如果你在下面键入它,对未来的访问者可能会有帮助:)你重新启动了rails服务器吗?@duhaime是的,我的问题是我缺少typeaheadah下的
    显示
    选项,很好!如果您找到了一个解决方案,如果您在下面键入它,可能会对未来的访问者有所帮助:)