简体   繁体   English

Ruby on Rails - 活动管理员:与全局变量冲突

[英]Ruby on Rails - Active Admin: Conflict with a Global Variable

On Rails 4 with the latest version of Active Admin (using Ransack). 在Rails 4上使用最新版本的Active Admin(使用Ransack)。 I have an Award model that I made available globally in my application_controller.rb: 我有一个我在application_controller.rb中全局提供的奖励模型:

  before_action :set_awards

  private
    def set_awards
      @awards = Award.all
    end

This is so I could have a global navigation dropdown listing all awards (on the public side). 这是我可以有一个列出所有奖项的全球导航下拉列表(在公共方面)。 I think this is conflicting with Active Admin. 我认为这与Active Admin存在冲突。 When I went to the Awards index page, I got the following error message: 当我进入奖项索引页面时,我收到以下错误消息:

Collection is not a paginated scope. Set collection.page(params[:page]).per(10) before calling :paginated_collection.

I am not sure what this means exactly but since this is the only model this happens to, I'm guessing it has something to do with my code in the app controller. 我不确定这究竟是什么意思,但由于这是唯一的模型,我猜这与我在app控制器中的代码有关。 I think my problem is similar to this question: 我认为我的问题类似于这个问题:

Rails 3.2 ActiveAdmin 'Collection is not a paginated scope.' Rails 3.2 ActiveAdmin'集合不是分页范围。' error 错误

...I would prefer not to change the model's label and the metasearch code provided doesn't work because (I think) Active Admin now uses Ransack. ...我宁愿不更改模型的标签,并且提供的元搜索代码不起作用,因为(我认为)Active Admin现在使用Ransack。 Thanks for any help. 谢谢你的帮助。

I have had the same problem and it was a variable name i was declaring at the application controller that was conflicting with the ActiveAdmin. 我有同样的问题,它是一个变量名称,我在应用程序控制器声明与ActiveAdmin冲突。

I have a model called Post and in my applicationController i had a method like this: 我有一个名为Post的模型,在我的applicationController中我有一个这样的方法:

def load_posts
  @posts = Post.all
end

It returned me the error you mention, so i fixed it changing the variable name to: 它返回了你提到的错误,所以我修复了它将变量名改为:

def load_posts
  @post_list = Post.all
end

Hope it helps. 希望能帮助到你。

May be you overwrited a local variable @awards in ApplicationController, but Active Admins is inheriting from the application controller. 可能是您在ApplicationController中覆盖了局部变量@awards,但Active Admins继承自应用程序控制器。 Just rename your variable and it will run. 只需重命名您的变量即可运行。

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

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