1

我安装了 Algolia 来为我的 rails 应用程序实现高效: algolia 搜索栏配置良好,因为我可以在我的数据库中找到我要查找的内容。用户可以使用带有自动完成功能的搜索栏查找图钉。

但我想让他们在结果上进入 pin 页面。

<!-- algolia search -->


 <input class="typeahead" type="text" placeholder="Search for users by name..." id="user-search" spellcheck="false" />

<!-- JQuery -->
<script src="//cdn.jsdelivr.net/jquery/1.11.1/jquery.min.js"></script>
<!-- Typahead.js is used to display the auto-completion menu -->
<script src="//cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.10.4/typeahead.bundle.min.js"></script>
<!-- Hogan.js is used to render the hits using Mustache.js templating -->
<script src="//cdn.jsdelivr.net/hogan.js/3.0.0/hogan.common.js"></script>
<!-- Algolia -->
<script src="//cdn.jsdelivr.net/algoliasearch/latest/algoliasearch.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {
    // Replace the following values by your ApplicationID and ApiKey.
    var algolia = new AlgoliaSearch('MYAPPID', 'MYAPPPW');
    // 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">' +
      '<div class="name">' +
        '{{{ _highlightResult.description.value }}} ' +
        '({{{ _highlightResult.details.value }}})' +
      '</div>' +
      '</div>');

    // typeahead.js initialization
    $('#user-search').typeahead(null, {
      source: index.ttAdapter({ hitsPerPage: 5 }),
      displayKey: 'email',
      templates: {
        suggestion: function(hit) {
          // select matching attributes only
          hit.matchingAttributes = [];
          for (var attribute in hit._highlightResult) {
            if (attribute === 'name' || attribute == 'company') {
              // 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);
        }
      }
    });
  });
</script>


<!-- /algolia search -->

我希望在单击结果时将用户重定向到 pin 显示视图。

所以我不知道如何将此链接呈现到 Hogan 链接中,因为我的 pin 的地址是例如:myapp.com/pins/2 for pin 2

解决方案:将链接添加到 hoogan 模板中,其中 slug 对我来说是带有friendly_id gem 的修改后的 URL。

// Mustache templating by Hogan.js (http://mustache.github.io/)
var template = Hogan.compile('<div class="hit">' +
'<a href="http://myapp.com/pins/{{{slug}}}" class="name">' +
    '{{{ _highlightResult.description.value }}} ' +
    '({{{ _highlightResult.details.value }}})' +
  '</a>' +
  '</div>');
4

1 回答 1

1

您必须修改 hoogan 模板才能添加链接,这里是使用linkedin 链接的示例http://jsfiddle.net/bxvnqtan/

// Mustache templating by Hogan.js (http://mustache.github.io/)
var template = Hogan.compile('<div class="hit">' +
'<a href="{{{linkedin}}}" class="name">' +
    '{{{ _highlightResult.description.value }}} ' +
    '({{{ _highlightResult.details.value }}})' +
  '</a>' +
  '</div>');
于 2014-11-03T09:18:53.747 回答