簡體   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