如何仅为活动管理员上的某些操作添加范围



我有一个经典的帖子属于用户关联。我想对所有操作(如#index)应用default_scope,以便它仅列出我的帖子。但是我希望能够在我转到#show操作时看到任何人的帖子,如果点击它的链接。

如何避免要应用于该操作的default_scope?

class Post < ActiveRecord::Base
  belongs_to :user
end
ActiveAdmin.register CertificationModel do
  controller do
    def scoped_collection
      current_user.posts
    end
  end
end

您可以为不希望调用scoped_collection的其他操作调用 super。

ActiveAdmin.register Post do
  controller do
    def scoped_collection
      if ['index', 'action2', 'action3'].include?(params[:action])
        current_user.posts
      else
        super
      end
    end
  end
end

解决方案很简单:保持scoped_collection并重新定义#show行动。

ActiveAdmin.register Post do
  controller do
    def show
      @post = Post.find params[:id]
    end
    def scoped_collection
      current_user.posts
    end
  end
end

最新更新