Ruby on Rails - HABTM NoMethodError

时间:2012-05-08 18:12:40

标签: ruby-on-rails has-and-belongs-to-many nomethoderror

我使用rails 3.2.3并且遇到HABTM关系问题。

基本上,我有一个用户模型,有一个带有角色模型的HABTM。

一切正常,但是,我现在想在User模型和酒店模型之间添加另一个HABTM关系。

除了当我尝试运行我的页面时,我得到一个(tail -f log / development.log):

NoMethodError (undefined method `[]' for :hotels:Symbol):
  app/models/user.rb:7
  config/routes.rb:6
  config/routes.rb:1

我的路线:

Oops::Application.routes.draw do #this is my line 1
  get "managers/index"

 root :to => "hotels#index"

  devise_for :users #this is my line 6
  resources :roles
  resources :users
  resources :managers
  resources :hotels

  ActiveAdmin.routes(self)

我的用户模型:

devise :database_authenticatable, :recoverable, (...)

has_and_belongs_to_many :roles, :hotels #This is my line 7, if I delete :hotels and all its references in this HABTM relationship, everything works

accepts_nested_attributes_for :roles

attr_accessible :username, :email, :password, :password_confirmation, :remember_me, :role_ids, :hotel_attributes

我的酒店型号:

has_and_belongs_to_many :facilities, :users
  has_one :address
  accepts_nested_attributes_for :facilities, :address, :users
  attr_accessible <all hotel attributes>, :facility_ids, :address_attributes, :user_ids

一切都在努力,直到这种新的关系。我根本无法在项目中运行任何页面。 有什么提示吗?

谢谢大家。

1 个答案:

答案 0 :(得分:2)

分割:

 has_and_belongs_to_many :roles, :hotels

分两行:

 has_and_belongs_to_many :roles
 has_and_belongs_to_many :hotels