Ruby on rails 让typeahead使用rails搜索
我让typeahead和search在rails 4应用程序中使用typeahead v0.10.5,但自从更新到v0.11.1后,它就坏了。我最初是从这个网站上的各种答案中获得代码的,但我并不真正了解到底发生了什么。我现在大部分时间都在工作,但下拉列表中填充了我模型中所有属性的文本,而不仅仅是我想要的名称。我想确保我做的事情是正确的,并希望能更好地了解正在发生的事情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
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'
“/typeahead/%QUERY”
。在其他示例中,我看到了类似于“/typeahead?q=%QUERY”
。有什么区别“建议”下拉列表正确显示的问题是,我缺少了作为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下的显示
选项,很好!如果您找到了一个解决方案,如果您在下面键入它,可能会对未来的访问者有所帮助:)