Em Sta
Em Sta

Reputation: 1706

Render partial in normal page

In my views i have an folder called categories and in that folder an file index. All worked good until i tried to render an partial in that site:

 <div class="row-fluid">
 <div class="span3">

<table class="table table-striped">
<tr>
<th>Kategory:</th>
<th></th>
</tr>

<% for category in @categories do %>
<tr>
....................................


 <div class="span9">
 <%= render "ebms/star" %>
 </div>
 </div>

Somehow i get the error

NoMethodError in Categories#index

Showing C:/Sites/rublesql/app/views/ebms/_star.html.erb where line #10 raised:

undefined method `each' for nil:NilClass

  <% for ebm in @ebms do %>
 11:   <tr>
 12:     <td><%= ebm.number %></td>    
 13:     <td><%= ebm.text %></td>

But i really dont know how i have to change my partial:

<table class="table table-striped">
 <tr>
<th>Nummer:</th>
<th>Text:</th>
<th>Beschreibung:</th>
<th></th>
<th></th>
</tr>

<% for ebm in @ebms do %>
<tr>
<td><%= ebm.number %></td>    
<td><%= ebm.text %></td>
<td><%= ebm.content %></td>
<td><%= ebm.star %></td>
<td><%= link_to 'Star', set_star_path(ebm), method: :put, confirm: 'Are you sure?' %>    </td>
</tr>
<% end %>
</table> 

Upvotes: 0

Views: 66

Answers (1)

davidb
davidb

Reputation: 8954

The partial expects that there is an instance variable calles @ebms which isnt defined so its nil and nil values cant be iterated like you try it in your partial. So you have either to change the partial so it expects an instance variable thats really given or you need to define @ebms in your controller so the partial can work with it.

In the controller you need to define the instance var like this:

@ebms = ModelName.where("<conditions>")

Its simple as that or you can change your partial so its eaven rendered if @ebms is not defined or nil...

<table class="table table-striped">
 <tr>
<th>Nummer:</th>
<th>Text:</th>
<th>Beschreibung:</th>
<th></th>
<th></th>
</tr>
<% if defined?(@ebms) && [email protected]? %>
  <% for ebm in @ebms do %>
  <tr>
  <td><%= ebm.number %></td>    
  <td><%= ebm.text %></td>
  <td><%= ebm.content %></td>
  <td><%= ebm.star %></td>
  <td><%= link_to 'Star', set_star_path(ebm), method: :put, confirm: 'Are you sure?' %>    </td>
  </tr>
  <% end %>
<% end %>
</table> 

Upvotes: 1

Related Questions