【问题标题】:rails NoMethodError: undefined method `hotel'rails NoMethodError:未定义的方法“酒店”
【发布时间】:2014-07-12 20:54:48
【问题描述】:

我是 Rails 新手。当我完成 Hartl 的教程时,类似的代码也有效。不同之处在于有一个 Micropost 模型,现在我为自己的应用创建了 Hotel 模型。

而且模型规格没有通过。我认为这个问题与关联有关,但谁知道......昨天花了一整天,但无法让他们通过。我很感激任何帮助。谢谢!

这是错误和我的代码:

Failures:

  1) Hotel 
     Failure/Error: before { @hotel = user.hotel.build(title: "Saffron") }
     NoMethodError:
       undefined method `hotel' for #<User:0xb3c2d2c>
     # ./spec/models/hotel_spec.rb:6:in `block (2 levels) in <top (required)>'

  2) Hotel 
     Failure/Error: before { @hotel = user.hotel.build(title: "Saffron") }
     NoMethodError:
       undefined method `hotel' for #<User:0xb5b1200>
     # ./spec/models/hotel_spec.rb:6:in `block (2 levels) in <top (required)>'

  3) Hotel 
     Failure/Error: before { @hotel = user.hotel.build(title: "Saffron") }
     NoMethodError:
       undefined method `hotel' for #<User:0xb7364b8>
     # ./spec/models/hotel_spec.rb:6:in `block (2 levels) in <top (required)>'

  4) Hotel 
     Failure/Error: before { @hotel = user.hotel.build(title: "Saffron") }
     NoMethodError:
       undefined method `hotel' for #<User:0xb42a9cc>
     # ./spec/models/hotel_spec.rb:6:in `block (2 levels) in <top (required)>'

  5) Hotel when user_id is not present 
     Failure/Error: before { @hotel = user.hotel.build(title: "Saffron") }
     NoMethodError:
       undefined method `hotel' for #<User:0xbc7ba40>
     # ./spec/models/hotel_spec.rb:6:in `block (2 levels) in <top (required)>'

  6) Hotel user 
     Failure/Error: before { @hotel = user.hotel.build(title: "Saffron") }
     NoMethodError:
       undefined method `hotel' for #<User:0xbefae4c>
     # ./spec/models/hotel_spec.rb:6:in `block (2 levels) in <top (required)>'

Finished in 1.11 seconds
33 examples, 6 failures

Failed examples:

rspec ./spec/models/hotel_spec.rb:10 # Hotel 
rspec ./spec/models/hotel_spec.rb:12 # Hotel 
rspec ./spec/models/hotel_spec.rb:11 # Hotel 
rspec ./spec/models/hotel_spec.rb:15 # Hotel 
rspec ./spec/models/hotel_spec.rb:19 # Hotel when user_id is not present 
rspec ./spec/models/hotel_spec.rb:13 # Hotel user

酒店.rb

class Hotel < ActiveRecord::Base
  belongs_to :user
  default_scope -> { order('created_at DESC') }
  validates :user_id, presence: true
end

用户.rb

class User < ActiveRecord::Base
  has_many :hotels
  before_save { email.downcase! }
  before_create :create_remember_token

  has_secure_password  
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  validates :password, length: { minimum: 6 }
  .....

hotel_spec.rb

require 'spec_helper'

describe Hotel do

  let(:user) { FactoryGirl.create(:user) }
  before { @hotel = user.hotel.build(title: "Saffron") }

  subject { @hotel }

  it { should respond_to(:title) }
  it { should respond_to(:user_id) }
  it { should respond_to(:user) }
  its(:user) { should eq user }

  it { should be_valid }

  describe "when user_id is not present" do
    before { @hotel.user_id = nil }
    it { should_not be_valid }
  end
end

Migration for Hotels 表

class CreateHotels < ActiveRecord::Migration
  def change
    create_table :hotels do |t|
      t.string :title
      t.integer :user_id

      t.timestamps
    end
    add_index :hotels, [:user_id, :created_at]
  end
end

【问题讨论】:

  • 试试before { @hotel = user.hotels.build(title: "Saffron") }。注意hotels不是hotel。
  • 非常感谢!这么简单的错误。一定是因为我今天从一开始就开始了酒店模式。

标签: ruby-on-rails nomethoderror


【解决方案1】:

欢迎来到轨道

您应该阅读guide on associations

此代码表示用户拥有许多酒店(带有s):

class User < ActiveRecord::Base
  has_many :hotels

因此,如果您要查找 user 的所有酒店,您可以使用代码 user.hotels,这是一个(许多酒店的)集合,因此您可以使用所有 ruby​​ 和 rails 集合方法。例如:

# This will output to your console the name of each hotels linked to that user
user.hotels.each do |hotel|
  puts hotel.name
end

现在,如果您正在寻找该系列中的第一家酒店,您可以使用users.hotels.first

还有更多的可能。阅读我在消息开头链接的指南,你也可以看看http://railscasts.com 有很多免费的好资料(有些是收费的,但对于基础学习,免费剧集非常好)

【讨论】:

  • 谢谢!链接非常有用!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
  • 2018-06-16
  • 2015-06-30
  • 1970-01-01
  • 2021-09-24
  • 1970-01-01
相关资源
最近更新 更多