我正在尝试在我的Rails 3应用程序中实现JQuery自动完成。以下是相关代码:
<%= event_form.text_field :evaluator_id, :class => "form-control" %>
$("#event_evaluator_id").autocomplete
source: "presenters_path"
match "events/presenters/" => "events#presenter_tokens", :as => 'presenters'
def presenter_tokens
if(@presenter_tokens.nil?)
@presenter_tokens = self.presenter_connections.order(:position).pluck(:learner_id).join(',')
end
@presenter_tokens
end
当我开始在文本字段中输入时,我在JS控制台中遇到的错误是:
GET http://localhost/events/2119/presenters_path?term=ma 404 (Not Found)
答案 0 :(得分:2)
您的问题出在javascript中。 Javascript无权访问辅助方法。
var startDate = '2012-04-01';
var endDate = '2014-11-01';
var start = new Date(Date.parse(startDate));
var end = new Date(Date.parse(endDate))
var dates = [];
for (var i = start.getFullYear(); i < end.getFullYear() + 1; i++) {
dates.push(i + '-' + '-01');
}
console.log(dates);
这里你告诉javascript源是字面意思“presenters_path”,这就是你在控制台中看到的附加到路径的内容。将源更改为实际路径$("#event_evaluator_id").autocomplete
source: "presenters_path"
,它应该到达正确的路径。