RAILS Instantsearch.js How to get the Ruby Object

I implemented Algolia’s instantsearch on my Rails app. I use it to display a list of product as search results.

For the instant search implementation I followed Algolia’s guide.

I created an Index called Products (on my Algolia account). It has a name, url, image, and an id (+ an objectID given by Algolia).

I can get the link like that :

<%= link_to "Voir",  CGI::unescape(product_path("{{objectID}}")) %>

My application.js file with the Algoli search widgets:

   var search = instantsearch({
  // Replace with your own values
  appId: "1JJ5DY0CLA",
  apiKey: 'e24882443747d61c496efc4e17288a36', // search only API key, no ADMIN key
  indexName: 'Idea',
  urlSync: true

    container: '#search-input',
    placeholder: 'Search for growth ideas',
    poweredBy: true

    container: '#hits',
    hitsPerPage: 10,
    templates: {
      item: getTemplate('hit'),
      empty: getTemplate('no-results')


function getTemplate(templateName) {
  return document.querySelector('#' + templateName + '-

The code inside my index.html.erb view looks like this:

# Comment: this is the code to structure each result sent by the API
<script type="text/html" id="hit-template">
    <div class="product">
       <div class="product-title">  
       <div class="product-link">
          <%= link_to "Voir", CGI::unescape(product_path("
          {{objectID}}")) %>

Here is the result sent by Algolia (got it from the browser), don’t know how to get the result variable inside the view/controller.

"hits": [
      "id": 1881,
      "female": true,
      "category": "Accessoires",
      "brand_id": 7,
      "url": "https://www.ekyog.com/foulard-noa-print-off-white/terra/geranium.html",
      "photos": [
      "price_cat": "0€ - 50€",
      "subcategory": "Echarpes & Foulards",
      "price_cents": 3400,

How can I retrieve the ruby object in my view . ?

I try to do it :

<% product = Product.find("{{objectID}}") %>
<% product = Product.find("{{id}}") %>

But it’s not working. How get I get the object inside the view/controller ?

It was replied on StackOverflow, so I’m just pasting the answer URL: https://stackoverflow.com/a/45236826/6289838

Thanks for your answer. Could you tell me how to use Hogan to get the products pictures, names…
This i s my product index. I wanna do the same thinks using instantsearch.


Hi @wedressfair,

Hogan.js follows the same specs as mustache.

The template you declared in the HTML view will be executed on the frontend for each of the objects in the hits array of the response. {{name}} will for instance be replaced with the name attribute of the record being rendered. {{photos.0}} will be replaced with the first value of the photos attribute array, and so on and so forth.

Does that clarify things?