2016-01-13 94 views
1

我在模型上定义了几个作用域,并在rails_admin中使用它们。RailsAdmin - 将参数传递到作用域

scope :foo , ->() { where(status: 'active')} 
... 
list do 
    scopes [nil, 'foo'] 

我想创建范围,我可以传递参数,然后从rails_admin加载这些范围。事情是这样的:

scope :bar , ->(query) { where(status: query)} 

但因为RA没有通过参数我不断收到

wrong number of arguments (0 for 1) 

有没有人有什么建议?我正在使用Rails 4.1.14与rails_admin 0.8.1和mongoid 5.0

回答

1

我通过创建自定义操作解决了这个问题,然后指定它使用索引模板。它显示在List,Add New和其他操作旁边,而不是下面的过滤器。

class Article 
    include Mongoid::Document 
    field :title, type: String 
    ... 
    belongs_to :user,  counter_cache: true 
    scope :by_user, ->(user) { where(user: user) } 
    ... 
end 
# in rails_admin.rb 
class Myarticles < RailsAdmin::Config::Actions::Base 
    RailsAdmin::Config::Actions.register(self) 
    register_instance_option :collection do 
    true 
    end 
    register_instance_option :only do 
    Article 
    end   
    register_instance_option :controller do 
    proc do 
     @objects = Article.by_user(current_user) 
     render :index 
    end 
    end 
end 

相关问题Rails_admin custom action specify template name

0

另一种方法是使用惨惨。你可以在能力文件中做到这一点:

def initialize(user) 
     can [:read], Profile, user_id: user.id 
    end