Rails:点击Algolia搜索自动完成中的结果

时间:2014-11-02 15:59:02

标签: javascript ruby-on-rails search algolia

我安装了Algolia来实现我的rails应用程序的高效: algolia搜索栏配置得很好,因为我可以在我的数据库中找到我要查找的内容。 用户可以使用搜索栏找到引脚,并在栏中自动完成。

但我想让他们将结果放在引脚页面上。

<!-- 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 show视图。

所以我不知道如何将这个链接渲染到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>');

1 个答案:

答案 0 :(得分: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>');