【问题标题】:Recurring Action template error ruby on railsRails 上的重复动作模板错误 ruby
【发布时间】: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

有什么建议吗?还是我看不到的东西?我已经尝试了几乎所有我现在能想到的东西。谢谢!

【问题讨论】:

  • 你为什么要分配@bookings 2x?第二个将覆盖第一个。

标签: ruby-on-rails ruby


【解决方案1】:

您没有在 show 方法中声明 @bookings 变量,这解释了崩溃。看来您是在 index 方法的第一行声明它。只要改变你声明的地方,就可以了。

另外,为避免更麻烦,您应该将 set_booking 方法重构为:

def set_booking
 @booking = Booking.find_by(id: params[:id])
end

默认.find() 如果找不到任何内容,会使您的视图崩溃

另一方面,.find_by 将返回一个空答案,因此它不会崩溃。此外,它更加通用,因为这样您可以从模型中设置的无数其他变量中进行搜索,即name

【讨论】:

  • 欢迎来到 Stackoverflow!这是一个很好的建议,但它并没有回答手头的具体问题,这可能就是为什么有人放弃投票的原因。考虑将其添加为评论,或编辑您当前的答案以解决 OP 询问的特定问题。快乐的黑客攻击。
  • 感谢您的指出。我错误地检查了在html 上迭代的变量。修正了一个正确的答案,并添加了我之前的答案作为避免未来并发症的建议。对不起我的错误
  • 谢谢!我没见过!我现在必须弄清楚如何在我的 html 文件上打印出信息。它完全没有阅读任何内容!
  • 让我看看能不能帮到你。我不知道你是如何声明你的@bookings 变量的,但是假设你使用了与第一次在提供的def index 上声明它时相同的方法,使用user_id。您确定该用户确实有预订吗?或者,我想您正在使用gem 'enumerize' 来获取预订状态。在您的枚举声明中,您是否将值设置为符号?如果是这样,在检查 booking.status 时,将其与小写值进行比较。如果您可以更新上面的文件,我可能会为您提供更多帮助。抱歉,回复太长了哈哈。
【解决方案2】:

按照 Rails 控制器操作中的约定(在您的情况下为 indexshow)渲染具有相同名称的视图,在您的情况下可能是 intex.html.erbshow.html.erb。在您的 show 方法中,您定义了两个变量:@bed@booking,在您的视图中 (show.html.erb),您尝试循环变量 @bookings,您尚未在控制器操作中定义该变量。因此,您会收到此错误。

看起来您需要将 show.html.erb 重命名为 index.html.erb,因为您要在其中列出预订及其信息。

【讨论】:

    猜你喜欢
    • 2017-06-02
    • 2013-08-21
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多