Javascript 当autocomplete为时,params返回nil
做自动完成。在controllerJavascript 当autocomplete为时,params返回nil,javascript,ruby-on-rails,Javascript,Ruby On Rails,做自动完成。在controllerformat.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 LIK
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]
(在控制器中)将出现nil
在控制器中:
def search
@words = Item.where("name LIKE '%#{params[:q]}%'")
respond_to do |format|
format.html
format.json { render json: @words.pluck(:name) }
end
end
$(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));
});
}
});
});
JS:
def search
@words = Item.where("name LIKE '%#{params[:q]}%'")
respond_to do |format|
format.html
format.json { render json: @words.pluck(:name) }
end
end
$(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));
});
}
});
});
使用插件
客户端自动完成上的提示工作正常。将术语作为查询参数传递:
$(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));
});
}
});
});
$(函数(){
$('input[name=“q”]”)。自动完成({
明查斯:1,
来源:功能(术语、响应){
term=term.toLowerCase();
$.getJSON('/search.json?q='+术语,函数(数据){
var匹配=[];
对于(i=0;i
您没有通过搜索传递术语。这就是为什么它是空的。请在之前为loop@PardeepDhingra,对不起,我不明白http://localhost:3000/search.json?q=fahe返回空数组。但我有法赫伯格的价值观。请求帮助使用rails中的ilike
@words=Item.where(“name-ILIKE”%{params[:q]}%”)
谢谢!我首先想到的是责任