显示视图返回错误:未定义方法“每个”

时间:2015-12-01 06:23:29

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2

我在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对象必须是一个数组。我需要更改什么才能使节目视图正常工作?提前谢谢!

3 个答案:

答案 0 :(得分:2)

问题和解释:

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

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

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

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

@advertisements = Advertisement.all

它为您提供了Advertisement个对象的集合。在这种情况下,您可以在视图中执行:@advertisements.each do ....

但是,对于show操作,@advertisementAdvertisement类的单个对象。所以,你不应该或不能尝试循环。您只需在@advertisement视图中显示该show个对象的相应属性/列值。

解决方案:

所以,将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>

答案 1 :(得分:0)

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>

答案 2 :(得分:0)

要显示所有广告,您应该索引方法并显示特定广告,您可以使用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>