Active Admin NoMethodError Error

*爱你&永不变心* 提交于 2019-12-08 02:50:19

问题


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)

Models:

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

回答1:


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:

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.

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


来源:https://stackoverflow.com/questions/11073642/active-admin-nomethoderror-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!