【发布时间】:2018-10-22 20:24:42
【问题描述】:
我一直有这个错误
ActionView::Template::Error(nil:NilClass 的未定义方法 `each'):
这是我的 show.html.erb
<% @bookings.each do |booking| %>
<% if booking.checkin_on > Date.today %>
<% if booking.status == "Confirmed" %>
<li class="dashboard">
<%= cl_image_tag(booking.bed.photo.path, width: 400, height: 300, crop: :fill, class: "pdt-image hidden-xs" ) %>
<div class='product-body'>
<h2><%= booking.bed.title %></h2>
<p>City: <strong><%= booking.bed.city %></strong></p>
<p>Address: <strong><%= booking.bed.address %></strong></p>
<p>Total price: <strong><%= booking.value %> €</strong></p>
</div>
<div>
<ul class="list-unstyled hidden-sm hidden-xs padded">
<li><strong>Your booking is confirmed !</strong></li>
<li class="text-right"><%= link_to "Delete this booking", booking_path(booking), method: :delete, class:"btn btn-default", data: {confirm: "Are you sure"} %> </li>
</ul>
</div>
</li>
<% end %>
<% if booking.status == "Canceled" %>
<li class="dashboard">
<%= cl_image_tag(booking.bed.photo.path, width: 400, height: 300, crop: :fill, class: "pdt-image hidden-xs" ) %>
<div class='product-body'>
<h2><%= booking.bed.title %></h2>
<p>City: <strong><%= booking.bed.city %></strong></p>
<p>Address: <strong><%= booking.bed.address %></strong></p>
<p>Total price: <strong><%= booking.value %> €</strong></p>
</div>
<div>
<ul class="list-unstyled hidden-sm hidden-xs padded">
<li><p><%= booking.bed.user.first_name%> canceled your booking </p></li>
</ul>
</div>
</li>
<% end %>
<% end %>
<% end %>
这是我的 my_bookings 控制器
class My::BookingsController < ApplicationController
def index
@bookings = Booking.where(user_id: current_user.id)
@bookings = Booking.all
@beds = Bed.all
end
def show
set_booking
@bed = @booking.bed
end
private
def set_booking
@booking = Booking.find(params[:id])
end
end
有什么建议吗?还是我看不到的东西?我已经尝试了几乎所有我现在能想到的东西。谢谢!
【问题讨论】:
-
你为什么要分配
@bookings2x?第二个将覆盖第一个。
标签: ruby-on-rails ruby