活动管理员NoMethodError错误 [英] Active Admin NoMethodError Error

查看:84
本文介绍了活动管理员NoMethodError错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im setting up active admin on my rails app. I ran the generator to create the user resource but when i click on the users link on the admin dashboard i get:

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)

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

I have no clue what is generating this or where the error is coming from.. Any ideas? Thanks! (please let me know if you need to see any other files)

型号:

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

省.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

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


推荐答案

此问题帮助我解决了我的问题。我必须阅读注释以弄清楚如何解决它,所以在这里提供答案:

This question helped me solve my problem. I had to read the comments to figure out how to solve it so I am providing the answer here:

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

If you have a model that contains a belongs_to relationship, Active Admin will not like it if you have a column name that matches your belongs to foreign key id column name.

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

For instance in this case your foreign key is 'city_id' but you also have a column named 'city'. Active Admin doesn't like this. And likewise it really doesn't make sense to have that column to begin with. Since you will access the city through the relationship.

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

To fix this you should create a migration that removes the city column.

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

这篇关于活动管理员NoMethodError错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆