gpt4 book ai didi

javascript - 当自动完成为 params 时返回 nil

转载 作者:太空宇宙 更新时间:2023-11-04 15:51:04 24 4
gpt4 key购买 nike

进行自动完成。在 Controller 中 format.json 返回一个名称数组。从 json 我获取自动完成的值。但是当我开始输入时,在日志中我有:

Started GET "/search.json" for 127.0.0.1 at 2017-03-30 09:41:28 +0300
Processing by PagesController#search as JSON
(3.1ms) SELECT "items"."name" FROM "items" WHERE (name LIKE '%%')
Completed 200 OK in 34ms (Views: 19.2ms | ActiveRecord: 3.1ms)

问题是params[:q](在 Controller 中)出现nil

Controller 中:

def search    
@words = Item.where("name LIKE '%#{params[:q]}%'")
respond_to do |format|
format.html
format.json { render json: @words.pluck(:name) }
end
end

JS:

$(function(){
$('input[name="q"]').autoComplete({
minChars: 1,
source: function(term, response){
$.getJSON('/search.json', function (data) {
term = term.toLowerCase();
var matches = [];
for (i = 0; i < data.length; i++)
if (~data[i].toLowerCase().indexOf(term)) matches.push(data[i]);
response(matches.slice(0,5));
});
}
});
});

.erb

<input id="hero-demo" autofocus type="text" name="q" style="width:100%;max-width:600px;outline:0">

使用插件 autocomplete

附注在客户端上,自动完成功能运行良好。

最佳答案

将术语作为查询参数传递:

$(function(){
$('input[name="q"]').autoComplete({
minChars: 1,
source: function(term, response) {
term = term.toLowerCase();
$.getJSON('/search.json?q='+ term, function (data) {
var matches = [];
for (i = 0; i < data.length; i++)
if (~data[i].toLowerCase().indexOf(term)) matches.push(data[i]);
response(matches.slice(0,5));
});
}
});
});

关于javascript - 当自动完成为 params 时返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43110674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com