Show View Returning Error: undefined method `each'

Show View Returning Error: undefined method `each'

我在 Stack Overflow 上研究了这个主题,但我似乎仍然无法弄清楚我做错了什么。

我的 show.html 视图文件抛出此错误:

undefined method `each' for #<Advertisement:0x007fce42676a70>

当我在本地浏览器中点击我的个人广告时。

我的show.html查看代码:

<h1><%= @advertisement.title %></h1>

<%= link_to "Edit advertisement", edit_advertisement_path, class: 'btn btn-success' %>
<%= link_to "Delete advertisement", @advertisement, method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this advertisement?' } %>

<div class="row">
  <div class="col-md-8">
    <p class="lead"><%= @advertisement.copy %></p>
    <% @advertisement.each do |advertisement| %>
    <div class="media">
      <div class="media-body">
        <h4 class="media-heading">
          <%= link_to advertisement.title, advertisement_path(@advertisement) %>
        </h4>
      </div>
    </div>
  <% end %>
  </div>
  <div class="col-md-4">
    <%= link_to "New Advertisement", new_advertisement_path(@advertisement), class: 'btn btn-success' %>
  </div>
</div>

我的广告控制器代码:

class AdvertisementsController < ApplicationController
  def index
    @advertisements = Advertisement.all
    # render('advertisements/index.html.erb')
  end

  def show
    # raise 'this is the show action'
    @advertisement = Advertisement.find(params[:id])
    # render 'advertisements/index.html.erb'
  end

  def edit
    @advertisement = Advertisement.find(params[:id])
  end

  def new
    @advertisement = Advertisement.new
  end

  def create
    @advertisement = Advertisement.new
    @advertisement.title = params[:advertisement][:title]
    @advertisement.copy = params[:advertisement][:copy]
    @advertisement.price = params[:advertisement][:price]

    if @advertisement.save
      flash[:notice] = "Advertisement was saved."
      redirect_to @advertisement
    else
      flash[:error] = "There was an error saving the advertisement. Please try again."
      render :new
    end
  end

  def update
    @advertisment = Advertisement.find(params[:id])
    @advertisment.title = params[:advertisement][:title]
    @advertisment.copy = params[:advertisement][:copy]
    @advertisment.price = params[:advertisement][:price]

    if @advertisement.save
      flash[:notice] = "Advertisement was updated."
      redirect_to @advertisement
    else
      flash[:error] = "Error saving advertisement. Please try again."
      render :new
    end
  end
end

通过研究,我认为我的问题是 advertisement 对象必须是一个数组。我需要更改什么才能使显示视图正常工作?提前致谢!

问题及解释:

undefined method `each' for #<Advertisement:0x007fce42676a70>

错误信息给了你提示。您可以对集合类型对象(例如数组或 ActiveRecordRelation 类型对象)调用 .each。在这种情况下,您不能(也不应该)调用 .each 因为没有任何东西可以循环或迭代。你这里只有一个对象:

@advertisement = Advertisement.find(params[:id])

对于您的索引视图,您在相应的控制器操作中具有以下内容:

@advertisements = Advertisement.all

这为您提供了 Advertisement 个对象的集合。在这种情况下,您可以执行以下操作:@advertisements.each do .... 在您看来。

但是,对于 show 操作,@advertisementAdvertisement class 的单个对象。因此,您不应该或不能尝试遍历它。您只需要在 show 视图中显示该 @advertisement 对象的相应 attributes/column 值。

解决方案:

因此,将您的 show.html.erb 更改为此(不使用 .each),这应该可以解决您的问题:

<div class="row">
  <div class="col-md-8">
    <p class="lead"><%= @advertisement.copy %></p>
        <div class="media">
          <div class="media-body">
            <h4 class="media-heading">
              <%= link_to @advertisement.title, advertisement_path(@advertisement) %>
            </h4>
          </div>
        </div>
  </div>
  <div class="col-md-4">
    <%= link_to "New Advertisement", new_advertisement_path(@advertisement), class: 'btn btn-success' %>
  </div>
</div>

在您的 show 视图中试试这个:

<h1><%= @advertisement.title %></h1>

<%= link_to "Edit advertisement", edit_advertisement_path, class: 'btn btn-success' %>
<%= link_to "Delete advertisement", @advertisement, method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this advertisement?' } %>

<div class="row">
  <div class="col-md-8">
    <p class="lead"><%= @advertisement.copy %></p>

    <div class="media">
      <div class="media-body">
        <h4 class="media-heading">
          <%= link_to @advertisement.title, advertisement_path(@advertisement) %>
        </h4>
      </div>
    </div>

  </div>
  <div class="col-md-4">
    <%= link_to "New Advertisement", new_advertisement_path, class: 'btn btn-success' %>
  </div>
</div>

要显示所有广告,您应该使用索引方法,而要显示特定广告,您可以使用 show 方法,例如:-

# Advertisement controller
def index
    @advertisements = Advertisement.all
end

def show
    @advertisement = Advertisement.find(params[:id])
end

index.html.erb

<div class="row">
    <div class="col-md-8">
        <% @advertisements.each do |advertisement| %>
            <p class="lead"><%= advertisement.copy %></p>
            <div class="media">
                <div class="media-body">
                    <h4 class="media-heading">
                        <%= link_to advertisement.title, advertisement_path(advertisement) %>
                    </h4>
                </div>
            </div>
            <%= link_to "Edit advertisement", edit_advertisement_path(advertisement), class: 'btn btn-success' %>
<%= link_to "Delete advertisement", advertisement, method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this advertisement?' } %>
        <% end %>
  </div>
  <div class="col-md-4">
      <%= link_to "New Advertisement", new_advertisement_path, class: 'btn btn-success' %>
  </div>
</div>

show.html.erb

<div class="row">
    <div class="col-md-8">
        <p class="lead"><%= @advertisement.copy %></p>
        <p class="lead"><%= @advertisement.title %></p>
    </div>
</div>