Rails gem rails3-jquery-autocomplete 如何按用户范围 [英] Rails gem rails3-jquery-autocomplete how to scope by user
问题描述
我正在使用 Rails gem rails3-jquery-autocomplete 为帖子添加类别.
I'm using the Rails gem rails3-jquery-autocomplete to add categories to posts.
我想将搜索限制为在结果中仅包含属于当前用户或帖子作者的类别.
I would like to restrict the search to include only categories that belong to the current user or post's author in the results.
文档说我可以指定一个范围:
The documentation says that I can specify a scope:
:范围
添加了使用范围的选项.在数组中传递范围.例如:范围=>[:scope1, :scope2]
Added option to use scopes. Pass scopes in an array. e.g :scopes => [:scope1, :scope2]
但我不确定如何在此处传递用户 ID?
But I'm not sure how I would pass the user id here?
这似乎是一个常见的场景,我是否遗漏了一些明显的东西?
It seems like a comon scenario, am I missing something obvious?
我找到了一个建议修改 get_item
方法的答案,但这似乎破坏了自动完成
I found an answer that suggests modifying the get_item
method, but that seems to break the auto-complete
推荐答案
在posts_controller
中:
def get_autocomplete_items(parameters)
items = super(parameters)
items = items.where(:user_id => current_user.id)
end
我先调用原来的get_autocomplete_items方法,然后通过current_user.id过滤掉结果.
I'm first calling the original get_autocomplete_items method, and then filtering out the results by current_user.id.
这个问题有帮助:Rails 3:仍使用 alias_method_chain?
这篇关于Rails gem rails3-jquery-autocomplete 如何按用户范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!