繁体   English   中英

活动管理员NoMethodError错误

[英]Active Admin NoMethodError Error

我在我的Rails应用程序上设置了活动管理员。 我运行了生成器来创建用户资源,但是当我单击管理仪表板上的用户链接时,我得到:

NoMethodError in Admin/users#index

Showing /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/activeadmin-0.4.4/app/views/active_admin/resource/index.html.arb where line #1 raised:

undefined method `city_id_contains' for #<MetaSearch::Searches::User:0x007fde92d69840>
Extracted source (around line #1):

1: render renderer_for(:index)

我不知道是什么原因导致的,或者错误是从哪里来的。 谢谢! (请让我知道是否需要查看其他文件)

楷模:

user.rb

  class User < ActiveRecord::Base
   rolify

  devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :province_id, :city_id
  belongs_to :province
  belongs_to :city

province.rb

 class Province < ActiveRecord::Base
      has_many :cities
      has_many :users
    end

city.rb

class City < ActiveRecord::Base
  belongs_to :province
  has_many :users
end

schema.rb

  create_table "users", :force => true do |t|
    t.string   "email",                  :default => "", :null => false
    t.string   "encrypted_password",     :default => "", :null => false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          :default => 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                             :null => false
    t.datetime "updated_at",                             :null => false
    t.string   "name"
    t.date     "date_of_birth"
    t.string   "address"
    t.string   "gender"
    t.integer  "zipcode"
    t.string   "city"
    t.string   "status"
    t.string   "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string   "unconfirmed_email"
    t.integer  "province_id"
    t.integer  "city_id"
  end

 create_table "provinces", :force => true do |t|
    t.string   "name"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

 create_table "cities", :force => true do |t|
    t.string   "name"
    t.integer  "province_id"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
  end

这个问题帮助我解决了我的问题。 我必须阅读注释以找出解决方法,因此在此提供答案:

如果您的模型包含一个belongs_to关系,并且您的列名与您属于所属外键ID的列名匹配,则Active Admin将不喜欢它。

例如,在这种情况下,您的外键是“ city_id”,但是您还有一列名为“ city”。 Active Admin不喜欢这样。 同样,以该列开头确实没有任何意义。 因为您将通过关系访问城市。

要解决此问题,您应该创建一个删除城市列的迁移。

class RemoveCityFromUser < ActiveRecord::Migration    
  def up 
    remove_column :users, :city
  end
end

暂无
暂无

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

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