我试图过滤搜索结果显示使用藻类宝石,这取决于当前用户是否携带‘管理’状态。
所有用户都可以创建客户,但是“admin”用户可以访问所有客户,而不管这些客户是否由该用户创建。我还在为我的“用户”模型使用devise。
客户控制器:
def index
if current_user.admin
@customers = Customer.all.order(id: :desc).paginate(:page => params[:page], :per_page => 4)
else
@customers = Customer.where(user: current_user).order(id: :desc).paginate(:page => params[:page], :per_page => 4)
end
end
我正在使用algolia宝石搜索customers#index中的客户。
搜索JS:
<!-- Algolia search JS -->
<script>
var client = algoliasearch('x', 'x');
var index = client.initIndex('Customer');
//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: 'name',
//hash of templates used when rendering dataset
templates: {
//'suggestion' templating function used to render a single suggestion
suggestion: function(suggestion) {
return `<span>${suggestion.first_name.value}"</span>` +;
}
}
});
</script>
我需要做的是过滤自动完成建议,这取决于登录用户是否是“admin”用户。这只需要过滤该特定会话的索引,以便多个同时登录的用户不会影响其他用户可用的客户索引。
发布于 2019-02-27 02:59:55
解决了!解决方案如下:我在“客户”索引中创建了“user_id”记录:
Customer.rb
attribute :user_id do
user.id if user
end
然后,我使用SrcParams.filter根据user_id过滤结果:
JS文件
var client = algoliasearch('x', 'x');
var index = client.initIndex('Customer');
let admin = document.getElementById("admin-field").innerText; // returns "true" for admins
let userId = parseInt(document.getElementById("id-field").innerText); // returns the integer value of the user's id
var srcParams = { hitsPerPage: 5}
// conditionally set filter based on admin
if (admin != "true") {
srcParams.filters = `user_id=${userId}`;
}
autocomplete('#aa-search-input', {
hint: false
}, {
// use the srcParams
source: autocomplete.sources.hits(index, srcParams),
displayKey: 'name',
templates: {
suggestion: function(suggestion) {
console.log(srcParams);
return `<span>${suggestion.first_name.value}"</span>` ;
}
}
});
发布于 2018-09-28 10:08:45
从设计上看,Algolia不是一个关系数据库。
它不知道是哪个user has_many customers
,还是一个customer belongs_to user
。它也不知道设计和用户是管理员。
要在Algolia中复制这种关系,您必须创建一个将关系保存在属性中的“平面对象”。
例如,您的“客户”索引可能有一个属性列表中可以查看它们的记录。您可以将其命名为viewable_by
或user_ids
,任何您想要的(如果您没有“用户”索引,那么这些可以是数据库中的id
,不管是什么都行):
// sample Customer record in Algolia
{
"name": "Ricky Bobby",
"viewable_by": [1, 55, 278]
}
接下来,我将依赖您从Rails传递您的current_user.admin
(true/false)和用户id
,以便您可以在JS条件中引用它。
然后,在“自动完成”中,创建一个条件参数集。如果current_user.admin
是false
,那么在用户id
上筛选
<!-- Algolia search JS -->
<
script >
var client = algoliasearch('BFBCTI275G', '5818cd87afb6c0ef5e4d5ec4ed6580d0');
var index = client.initIndex('Customer');
// create variable
var srcParams = {
hitsPerPage: 5
}
// conditionally set filter based on admin
// your job to get admin boolean and user id into JS
if (!current_user.admin) {
srcParams.filters = 'viewable_by: current_user.id';
}
autocomplete('#aa-search-input', {
hint: false
}, {
// use the srcParams
source: autocomplete.sources.hits(index, srcParams),
displayKey: 'name',
templates: {
suggestion: function(suggestion) {
return `<span>${suggestion.first_name.value}"</span>` + ;
}
}
}); <
/script>
https://stackoverflow.com/questions/52555454
复制相似问题