Ruby on rails RAILS Algolia如何获取Ruby对象Instantsearch.js
我在Rails应用程序上实现了Algolia的instantsearch。我用它来显示产品列表作为搜索结果 对于即时搜索的实现,我遵循了Algolia的指南 我创建了一个名为Products的索引(在我的Algolia帐户上)。它有名称、url、图像和id(+由Algolia提供的objectID) 带有Algoli搜索小部件的My application.js文件:Ruby on rails RAILS Algolia如何获取Ruby对象Instantsearch.js,ruby-on-rails,algolia,instantsearch.js,Ruby On Rails,Algolia,Instantsearch.js,我在Rails应用程序上实现了Algolia的instantsearch。我用它来显示产品列表作为搜索结果 对于即时搜索的实现,我遵循了Algolia的指南 我创建了一个名为Products的索引(在我的Algolia帐户上)。它有名称、url、图像和id(+由Algolia提供的objectID) 带有Algoli搜索小部件的My application.js文件: var search = instantsearch({ // Replace with your own valu
var search = instantsearch({
// Replace with your own values
appId: "1JJ5DY0CLA",
apiKey: 'e24882443747d61c496efc4e17288a36', // search only API key, no ADMIN key
indexName: 'Idea',
urlSync: true
});
search.addWidget(
instantsearch.widgets.searchBox({
container: '#search-input',
placeholder: 'Search for growth ideas',
poweredBy: true
})
);
search.addWidget(
instantsearch.widgets.hits({
container: '#hits',
hitsPerPage: 10,
templates: {
item: getTemplate('hit'),
empty: getTemplate('no-results')
}
})
);
search.start();
function getTemplate(templateName) {
return document.querySelector('#' + templateName + '-
template').innerHTML;
}
my index.html.erb视图中的代码如下所示:
# 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">
<h3>{{title}}</h3>
</div>
<div class="product-link">
<%= link_to "Voir", CGI::unescape(product_path("
{{objectID}}")) %>
</div>
</div>
我可以得到这样的链接:
<%= link_to "Voir", CGI::unescape(product_path("{{objectID}}")) %>
如何在视图中检索ruby对象
我试着去做:
<% product = Product.find("{{objectID}}") %>
或
但它不起作用。如何获取视图/控制器中的对象?您的ERB代码仅在页面渲染时计算。 到的
链接将起作用,因为这只是生成一个字符串(例如,/products/{{{objectID}}
),该字符串稍后将由前端更新
您不能使用这些变量来实际使用Ruby代码处理它们。实际上,使用instantsearch.js
,您可以在不重新加载页面的情况下动态检索结果,这意味着您只能访问JavaScript上下文。谢谢您的回答,所以我必须使用REACT来完成此操作?您的命中模板已经使用了一个名为Hogan的模板引擎:。
<% product = Product.find("{{objectID}}") %>
<% product = Product.find("{{id}}") %>