加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 22:22:17 所属栏目:百科 来源:网络整理
导读:这很简单,不应该是一个问题,但我不明白这里发生了什么. 我有以下代码 class DashboardController ApplicationController def bookings @bookings = Booking.all endend /views/dashboard/bookings.html.erb %= render 'booking',:collection = @bookings % /
这很简单,不应该是一个问题,但我不明白这里发生了什么.

我有以下代码

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:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读