ruby-on-rails-3.2 – Rails渲染部分与:集合

这很简单,不应该是一个问题,但我不明白这里发生了什么.

我有以下代码

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:

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...