Ruby on Rails-#

时间:2018-10-02 12:00:34

标签: ruby-on-rails controller activeadmin

当我在application_controller.rb中添加一些代码时,我在Active Admin中遇到错误。错误是(当我尝试以活动管理员身份访问HomeConfig时):

undefined method 'except' for #<HomeConfig:0x007f8877cfda58>

在这种情况下,当我将这段代码放在application_controller.rb中时会发生错误:

class ApplicationController < ActionController::Base

  protect_from_forgery with: :exception

  # Code that cause the problem
  before_filter :contact_info, :home_config

  def contact_info
    @contact_infos = ContactInfo.all
  end

  def home_config
    @home_configs = HomeConfig.last
  end
end

以及终端中的错误日志:

Started GET "/alumni/admin/home_configs" for 127.0.0.1 at 2018-10-02 09:07:38 -0300
Processing by Admin::HomeConfigsController#index as HTML
  HomeConfig Load (0.4ms)  SELECT  `home_configs`.* FROM `home_configs`   ORDER BY `home_configs`.`id` DESC LIMIT 1
  AdminUser Load (0.3ms)  SELECT  `admin_users`.* FROM `admin_users`  WHERE `admin_users`.`id` = 1  ORDER BY `admin_users`.`id` ASC LIMIT 1
  Rendered /Users/Atua/.rvm/gems/ruby-2.1.3@facensAlumni/bundler/gems/activeadmin-dce083189c46/app/views/active_admin/resource/index.html.arb (598.9ms)
Completed 500 Internal Server Error in 644ms

NoMethodError - undefined method `except' for #<HomeConfig:0x007f887c002d58>:

Started POST "/__better_errors/32ad65699eb25cf6/variables" for 127.0.0.1 at 2018-10-02 09:07:39 -0300
  ContactInfo Load (0.3ms)  SELECT `contact_infos`.* FROM `contact_infos`

如果我撤回此代码并重新启动服务器,则一切正常。有人知道发生了什么事吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

@home_configs = HomeConfig.last

这正在创建错误。 ActiveAdmin期望@home_configs是一个数组。而您的代码HomeConfig.last仅返回最后一个HomeConfig。

您需要将HomeConfig.last更改为HomeConfig.all

尝试并反馈。