与show.html.erb关联的Rspec错误

时间:2011-12-22 17:06:27

标签: ruby-on-rails rspec

以下是rspec的错误:

 1) LeaseBookingsController 'show' should grant view for the team lead of the booking
     Failure/Error: get 'show', :id => booking.id
     ActionView::Template::Error:
       undefined method `name' for nil:NilClass
     # ./app/views/lease_bookings/show.html.erb:14:in `_app_views_lease_bookings_show_html_erb___921278774_32107152'
     # ./spec/controllers/lease_bookings_controller_spec.rb:149:in `block (3 levels) in <top (required)>'

以下是show.html.erb中导致错误的行:

<p>
  <b>Equip Name:</b>
  <%= @lease_booking.lease_item.name %>
</p>

协会:

lease_booking属于lease_item,lease_item有许多lease_bookings。 lease_booking模型中的关联是:

class LeaseBooking < ActiveRecord::Base
  belongs_to :lease_item

在lease_item模型中,它是:

class LeaseItem < ActiveRecord::Base
  has_many :lease_booking

如果在lease_item中删除has_many关联,则错误相同。在我看来,实际检索的数据可能有多个记录,但关联只需要一条记录。

感谢。

0 个答案:

没有答案