0

I am using Rails to create my app, but javascript to implement the search. This is what I have for my Algolia autocomplete. I am on the free ('Hacker') plan with Algolia, and I want to add a footer. Is there an easy way to do this? What I have below (which I found at: https://github.com/algolia/autocomplete.js/blob/master/README.md) is not working.

var client = algoliasearch("<%= ENV['ALGOLIA_API'] %>", "<%= ENV['ALGOLIA_SECRET'] %>");
var index = client.initIndex('User');

//initialize autocomplete on search input (ID selector must match)
autocomplete('#aa-search-input',
{ hint: false }, [{
    source: autocomplete.sources.hits(index, {hitsPerPage: 5}),
    //value to be displayed in input control after user's suggestion selection
    displayKey: function(suggestion) { return suggestion.first_name + " " + suggestion.last_name},
    //hash of templates used when rendering dataset
 templates: {
        //'suggestion' templating function used to render a single suggestion
        suggestion: function(suggestion) {
          var link = "<form action='<%= ENV['HOST'] %>users/" + suggestion.id + "/friend_requests' method='post' id='addfriend' style='color: lightgreen'> <input name='authenticity_token' value='<%= form_authenticity_token %>' type='hidden'> <button type='submit' name='user_id' value='" + suggestion.id + "' class='btn-link'>Add</button></form>";
          var card = "<span>" + "<img src=<%= ENV['CLOUDINARY_SECOND_URL']  %>" + suggestion.photo.path + " alt='' class='avatar'> " +
            suggestion._highlightResult.first_name.value + " " + suggestion._highlightResult.last_name.value + "</span><span>" + link +"</span>";

          return card

        }
    }
    footer: '<span class="branding">Powered by <img src="https://www.algolia.com/assets/algolia128x40.png" /></span>'
}]).on('autocomplete:selected', function(event, suggestion, dataset) {
    var url = "<%= ENV['HOST'] %>users/";
    window.location.assign(url + suggestion.id)});
julianne
  • 13
  • 4

1 Answers1

0

You need to put footer inside of the templates object. Like that:

templates: {
    //'suggestion' templating function used to render a single suggestion
    suggestion: function(suggestion) {
        var link = "<form action='<%= ENV['HOST'] %>users/" + suggestion.id + "/friend_requests' method='post' id='addfriend' style='color: lightgreen'> <input name='authenticity_token' value='<%= form_authenticity_token %>' type='hidden'> <button type='submit' name='user_id' value='" + suggestion.id + "' class='btn-link'>Add</button></form>";
        var card = "<span>" + "<img src=<%= ENV['CLOUDINARY_SECOND_URL']  %>" + suggestion.photo.path + " alt='' class='avatar'> " +
        suggestion._highlightResult.first_name.value + " " + suggestion._highlightResult.last_name.value + "</span><span>" + link +"</span>";

        return card

    },
    footer: '<span class="branding">Powered by <img src="https://www.algolia.com/assets/algolia128x40.png" /></span>'
}

Right now you have it outside, so it does not take footer template into account.

Jan Petr
  • 685
  • 5
  • 11