Cameron Bass Cameron Bass - 7 months ago 4
Ruby Question

Getting an undefined method `each' for nil:NilClass

I understand what this error is saying but I don't know why I'm getting it? I'm trying to display a table with the index action, everything seems pretty standard but for some reasons this error has occurred. I'll post my code for clarity.

view

<table class="table table-striped">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phone Number</th>
</tr>
</thead>
<tbody>
<% @subsriber.each do |subsriber| %>
<tr>
<td><%= subscriber.first_name %></td>
<td><%= subscriber.last_name %></td>
<td><%= subscriber.email %></td>
<td><%= subscriber.phone_number %></td>
</tr>
<% end %>
</tbody>
</table>


controller

def index
@subscriber = Subscriber.all
end


I know I must be missing something simple. Thanks for the help!

Answer

A typo error, in your controller you defined @subscriber but used @subsriber in the view!

Btw, conventionally, You may use @subscribers instead of @subscriber, it is clearer!

Comments