【发布时间】:2016-05-02 18:17:47
【问题描述】:
我将algolia search 用于我的rails 应用程序,我使用typehead.js 设置了自动完成方法。但是当我在 URL 中使用大写字母时,我无法正确重定向...这是我的代码:
// replace YourIndexName by the name of the index you want to query.
var index = algolia.initIndex('Pin');
// Mustache templating by Hogan.js (http://mustache.github.io/)
var template = Hogan.compile('<div class="hit">' +
'<a href="http://yhumans.com/{{{twitter}}}">'
+
'<div class="small-text">' +
'{{{ _highlightResult.name.value }}} ' +
'</div>' +
'<div class="small-text">' +
'@' +
'{{{ _highlightResult.twitter.value }}} ' +
'</div>' +
'</a>' +
'</div>');
// typeahead.js initialization
$('#user-search').typeahead(null, {
source: index.ttAdapter({ hitsPerPage: 5 }),
displayKey: 'twitter',
templates: {
suggestion: function(hit) {
// select matching attributes only
hit.matchingAttributes = [];
for (var attribute in hit._highlightResult) {
if (attribute === 'name' || attribute == 'twitter') {
// already handled by the template
continue;
}
// all others attributes that are matching should be added in the matchingAttributes array
// so we can display them in the dropdown menu. Non-matching attributes are skipped.
if (hit._highlightResult[attribute].matchLevel !== 'none') {
hit.matchingAttributes.push({ attribute: attribute, value: hit._highlightResult[attribute].value });
}
}
// render the hit using Hogan.js
return template.render(hit);
}
}
});
问题在于我用来让用户点击结果以访问页面的这行代码:
<a href="http://yhumans.com/{{{twitter}}}">'
事实上,我的一些用户在他们的推特用户名中使用了大写字母,所以我无法在搜索结果中正确重定向到他们的个人资料。
让我解释一下:http://yhumans.com/myname 是一个正确的 URL。 http://yhumans.com/MyName 是错误的 URL 我尝试为该变量使用小写字母:“twitter”。但我找不到正确的方法。
我知道一种方法是将 twitter 变量小写。但问题是功能“小写!”在 js 中似乎不起作用。
有什么想法吗?
【问题讨论】:
标签: javascript ruby-on-rails search algolia