简体   繁体   English

在activeadmin资源中需要范围

[英]Require scope in activeadmin resource

I am using activeadmin and need to figure out how to require a scope to only show records pertaining to the current user. 我正在使用activeadmin并且需要弄清楚如何要求范围仅显示与当前用户相关的记录。

I also have other scopes that users can select, but those scopes need to be "pre-scoped" so to speak so that only records belonging to that user are available at any given time. 我还有其他用户可以选择的范围,但这些范围需要“预先确定”,以便在任何给定时间只有属于该用户的记录可用。

I hope this makes sense. 我希望这是有道理的。 I'm fairly new to all of this, so i'm not real sure where to start. 我对所有这些都很新,所以我不确定从哪里开始。 Thanks in advance for any help. 在此先感谢您的帮助。

Did you try scoping with scope_to :current_user ? 您是否尝试使用scope_to :current_user

AA has some examples with docs . AA有一些文档示例。 Here they are http://activeadmin.info/docs/2-resource-customization.html#scoping_the_queries 这里是http://activeadmin.info/docs/2-resource-customization.html#scoping_the_queries

current_user is helper method to get currently logged in user (currend_admin_user is default I think) code from AA initializer current_user是帮助方法从AA初始化程序获取当前登录用户(currend_admin_user是我认为的默认值)代码

# This setting changes the method which Active Admin calls
# to return the currently logged in user.
config.current_user_method = :current_user

If you had some kind of metod in your model that use your logged in user you can do something like that 如果您的模型中有某种使用登录用户的metod,您可以执行类似的操作

controller do
      def scoped_collection
        Post.some_method(current_user)
        #or for example Post.select(current_user.visible_posts_columns) ... etc
      end
    end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM