我在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
对象必须是一个数组。我需要更改什么才能使节目视图正常工作?提前谢谢!
答案 0 :(得分:2)
undefined method `each' for #<Advertisement:0x007fce42676a70>
错误消息为您提供提示。您可以在集合类型对象(如数组或ActiveRecordRelation类型对象)上调用.each
。在这种情况下,您不能(也不应该)调用.each
,因为没有任何东西可以循环或遍历。这里只有一个对象:
@advertisement = Advertisement.find(params[:id])
对于索引视图,您在相应的控制器操作中有以下内容:
@advertisements = Advertisement.all
它为您提供了Advertisement
个对象的集合。在这种情况下,您可以在视图中执行:@advertisements.each do ....
。
但是,对于show
操作,@advertisement
是Advertisement
类的单个对象。所以,你不应该或不能尝试循环。您只需在@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>