在 Rails 应用中使用 link_to 中的 algolia 索引属性
Using algolia index attributes in link_to in Rails app
我在我的 Rails 应用程序上实现了 Algolia 的即时搜索。我用它来显示产品列表作为搜索结果。
每个产品上都有一个保存按钮。我想检索每个结果的 ID 以将其传递到 link_to
标记中。
对于即时搜索实现,我遵循了 Algolia's guide。
我创建了一个名为 Products 的索引(在我的 Algolia 帐户上)。它有一个标题和一个 ID(+ Algolia 给出的 objectID)。
我的 application.js 文件和 Algoli 搜索小部件:
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;
}
我的 index.html.erb 视图中的代码如下所示:
<div id="hits"></div>
# 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-save">
# Comment: where I'd like to replace product_id by the result id
<%= link_to "save", product_path(product_id, current_user), method: :post %>
</div>
</div>
如何检索结果的 ID 以将其传递给 link_to 标记?
这里是Algolia发送的结果(从浏览器获取),不知道如何获取view/controller.
里面的result变量
{
"results":
[
{
"hits":
[
{
"title":"Product title1",
"id":65,
"objectID":"344300262"
},
{
"title":"Product title2",
"id":66,
"objectID":"344300263"
}
]
}
]
第一次测试:
从 algolia 示例(见上面我的代码)中,我认为我可以像这样将结果属性检索到 html 中:{{id}}
。但我不确定它是什么样的"object"。
我可以用<%= "{{id}}" %>
在页面上显示id。但是在 Ruby 它被解释为一个字符串 {{id}}
所以我不能像下面这样使用它:
<%= link_to "save", product_path("{{id}}", current_user), method: :post %>
此外,我看到有一个 get_object
方法可以在我的索引上使用来检索整个 Algolia 的 object,如下所示:
index.get_object("objectID")
,objectID
是一个 integer
。
但是关于id,我不知道如何获取当前显示结果的objectID。
如果我的问题需要任何改进,我很乐意让它更容易理解。告诉我吧。
您不能在 JavaScript 代码中使用动态助手。
原因是 link_to
在服务器渲染时只被调用一次。
当页面随后被提供时,您的 link 已经转换为 <a>
标签。
当您使用instantsearch.js
时,您不会再次调用服务器,页面仅使用JavaScript刷新,这意味着您将无法使用link_to
在每个渲染器上。
但是,您 "{{id}}"
确实走在了正确的道路上。这里唯一的问题是 _path
助手正在转义参数,如果您使用用户数据生成路径,这是有意义的,但在这种情况下不是必需的。
因此您可以取消转义生成的路径,如下所示:
<%= link_to "save", CGI::unescape(product_path("{{id}}", current_user)), method: :post %>
或
<%= link_to "save", CGI::unescape(product_path("{{objectID}}", current_user)), method: :post %>
但是,我不保证 POST 方法会起作用,您可能需要以我不知道的方式重新绑定那些 <a>
标签。
我在我的 Rails 应用程序上实现了 Algolia 的即时搜索。我用它来显示产品列表作为搜索结果。
每个产品上都有一个保存按钮。我想检索每个结果的 ID 以将其传递到 link_to
标记中。
对于即时搜索实现,我遵循了 Algolia's guide。
我创建了一个名为 Products 的索引(在我的 Algolia 帐户上)。它有一个标题和一个 ID(+ Algolia 给出的 objectID)。
我的 application.js 文件和 Algoli 搜索小部件:
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;
}
我的 index.html.erb 视图中的代码如下所示:
<div id="hits"></div>
# 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-save">
# Comment: where I'd like to replace product_id by the result id
<%= link_to "save", product_path(product_id, current_user), method: :post %>
</div>
</div>
如何检索结果的 ID 以将其传递给 link_to 标记?
这里是Algolia发送的结果(从浏览器获取),不知道如何获取view/controller.
里面的result变量 {
"results":
[
{
"hits":
[
{
"title":"Product title1",
"id":65,
"objectID":"344300262"
},
{
"title":"Product title2",
"id":66,
"objectID":"344300263"
}
]
}
]
第一次测试:
从 algolia 示例(见上面我的代码)中,我认为我可以像这样将结果属性检索到 html 中:{{id}}
。但我不确定它是什么样的"object"。
我可以用<%= "{{id}}" %>
在页面上显示id。但是在 Ruby 它被解释为一个字符串 {{id}}
所以我不能像下面这样使用它:
<%= link_to "save", product_path("{{id}}", current_user), method: :post %>
此外,我看到有一个 get_object
方法可以在我的索引上使用来检索整个 Algolia 的 object,如下所示:
index.get_object("objectID")
,objectID
是一个 integer
。
但是关于id,我不知道如何获取当前显示结果的objectID。
如果我的问题需要任何改进,我很乐意让它更容易理解。告诉我吧。
您不能在 JavaScript 代码中使用动态助手。
原因是 link_to
在服务器渲染时只被调用一次。
当页面随后被提供时,您的 link 已经转换为 <a>
标签。
当您使用instantsearch.js
时,您不会再次调用服务器,页面仅使用JavaScript刷新,这意味着您将无法使用link_to
在每个渲染器上。
但是,您 "{{id}}"
确实走在了正确的道路上。这里唯一的问题是 _path
助手正在转义参数,如果您使用用户数据生成路径,这是有意义的,但在这种情况下不是必需的。
因此您可以取消转义生成的路径,如下所示:
<%= link_to "save", CGI::unescape(product_path("{{id}}", current_user)), method: :post %>
或
<%= link_to "save", CGI::unescape(product_path("{{objectID}}", current_user)), method: :post %>
但是,我不保证 POST 方法会起作用,您可能需要以我不知道的方式重新绑定那些 <a>
标签。