NameError in Contacts#index
Showing C:/mycontact/app/views/contacts/index.html.erb where line #43 raised:
undefined local variable or method `contact' for #<#<Class:0x3b7eee0>:0x3b7c738>
Rails.root: C:/mycontact
以下是index.html.erb文件:
<div class="panel panel-default">
<table class="table">
<% @contacts.each do |contact| %>
<tr>
<td class="middle">
<div class="media">
<div class="media-left">
<a href="#">
<img class="media-object" src="http://placehold.it/100x100" alt="...">
</a>
</div>
<div class="media-body">
<h4 class="media-heading"><%= contact.name %></h4>
<address>
<strong><%= contact.company %></strong><br>
<%= contact.email %>
</address>
</div>
</div>
</td>
<td width="100" class="middle">
<div>
<%= link_to edit_contact_path(contact), class: "btn btn-circle btn-default btn-xs", title: "Edit" do %>
<i class="glyphicon glyphicon-edit"></i>
<% end %>
<%= link_to contact_path(contact), :method => :delete, class: "btn btn-circle btn-danger btn-xs", title: "Delete" , data: { confirm: "Are you sure?"} do %>
<i class="glyphicon glyphicon-remove"></i>
<% end %>
</div>
</td>
</tr>
<% end %>
<tr>
<td class="middle">
<div class="media">
<div class="media-left">
<a href="#">
<%= image_tag contact.avatar.url(:thumb), class: "media-object" %>
</a>
</div>
<div class="media-body">
<h4 class="media-heading">Contact 2</h4>
<address>
<strong>Job 2</strong><br>
contact2@sample.com
</address>
</div>
</div>
</td>
<td width="100" class="middle">
<div>
<a href="#" class="btn btn-circle btn-default btn-xs" title="Edit">
<i class="glyphicon glyphicon-edit"></i>
</a>
<a href="#" class="btn btn-circle btn-danger btn-xs" title="Edit">
<i class="glyphicon glyphicon-remove"></i>
</a>
</div>
</td>
</tr>
</table>
</div>
<div class="text-center">
<%= will_paginate %>
</div>
答案 0 :(得分:2)
<%= image_tag contact.avatar.url(:thumb), class: "media-object" %>
不在循环中,所以联系是未定义的。
要解决此问题,您应将<% end %>
移至文件末尾。