我在我的 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
省.rb
class Province < ActiveRecord::Base
has_many :cities
has_many :users
end
城市.rb
class City < ActiveRecord::Base
belongs_to :province
has_many :users
end
架构.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”的列。活跃的管理员不喜欢这样。同样,以该专栏开头确实没有意义。因为您将通过关系访问这座城市。
要解决此问题,您应该创建一个移除城市列的迁移。
class RemoveCityFromUser < ActiveRecord::Migration
def up
remove_column :users, :city
end
end
关于ruby-on-rails - 事件管理员 NoMethodError 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11073642/