Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 显示视图返回错误:未定义的方法'each';_Ruby On Rails_Ruby_Ruby On Rails 3_Ruby On Rails 4_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails 显示视图返回错误:未定义的方法'each';

Ruby on rails 显示视图返回错误:未定义的方法'each';,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,Ruby On Rails 3.2,我在这里研究了堆栈溢出这个主题,但我似乎仍然无法找出我做错了什么 My show.html视图文件引发以下错误: undefined method `each' for #<Advertisement:0x007fce42676a70> 未定义的方法“each”# 当我在本地浏览器中点击我的个人广告时 My show.html查看代码: <h1><%= @advertisement.title %></h1> <%= link_to "

我在这里研究了堆栈溢出这个主题,但我似乎仍然无法找出我做错了什么

My show.html视图文件引发以下错误:

undefined method `each' for #<Advertisement:0x007fce42676a70>
未定义的方法“each”#
当我在本地浏览器中点击我的个人广告时

My 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

我的广告控制器代码:

<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
类广告控件
通过研究,我认为我的问题是
广告
对象必须是一个数组。要使show view正常工作,我需要更改什么?提前谢谢

问题及解释: 对于索引视图,在相应的控制器操作中有以下内容:

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

但是,对于
show
操作,
@advision
advision
类的单个对象。所以,你不应该也不可以尝试通过这个循环。您只需要在
show
视图中显示该
@advision
对象的相应属性/列值

解决方案: 因此,将
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>


显示视图中尝试以下操作:

<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>


对于显示所有广告,您应该使用索引方法;对于显示特定广告,您可以使用如下显示方法:-

# 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>


谢谢!我不知道为什么当只有一个广告在播放时,我试图在一个节目视图上循环播放。在阅读了你的回答后才有意义。谢谢你的帮助!