这很简单,不应该是一个问题,但我不明白这里发生了什么.
我有以下代码
class DashboardController < ApplicationController def bookings @bookings = Booking.all end end
/views/dashboard/bookings.html.erb
<%= render 'booking',:collection => @bookings %>
/views/dashboard/_booking.html.erb
<%= booking.booking_time %>
我收到以下错误
undefined method `booking_time' for nil:NilClass
但是,如果我在/views/dashboard/_bookings.html.erb中这样做
<% @bookings.each do |booking| %> <%= render 'booking',:booking => booking %> <% end %>
我得到(正确)
2012-12-19 09:00:00 UTC 2012-12-28 03:00:00 UTC
这里发生了什么?我真的想使用:这里定义的集合
http://guides.rubyonrails.org/layouts_and_rendering.html
解决方法
这是一个老问题,只是其他人正在努力解决问题
When a partial is called with a pluralized collection,then the
individual instances of the partial have access to the member of the
collection being rendered via a variable named after the partial.
从http://guides.rubyonrails.org/layouts_and_rendering.html
在OP上面的例子中,由于部分被称为预订而不是预订,因此Rails未能推断在部分中使用的成员名称,因此需要明确地传递它:as: