2016-01-13 91 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