Topic: undefined local variable or method

I am trying to tidy up an index page I'm working on. Here is the one that works:

<h1>Listing cities</h1>
<%= link_to 'New city', new_city_path %>
<table>
<tr>
    <th>Name</th>
    <th>Ghome</th>
    <th>Image url</th>
  </tr>

<% for city in @cities %>
  <tr>
    <td><%=h city.name %></td>
    <td><%=h city.ghome %></td>
    <td><%=h city.image_url %></td>
    <td><%= link_to 'Show', city %></td>
    <td><%= link_to 'Edit', edit_city_path(city) %></td>
    <td><%= link_to 'Destroy', city, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>

I have altered this page to try to make it look better, but I get an error.  It says "undefined local variable or method 'city' for ..."

Here is the code I use that gives me the problems:

<div id="city-list">
  <h1>Listing Cities</h1>
  <%= link_to 'New city', new_city_path %>
    <table>
    <% for cities in @cities %>
      <tr class="<%= cycle('list-line-odd', 'list-line-even') %>">
     
        <td>
          <%= image_tag city.image_url, :class => 'list-image' %>
        </td>

        <td class="list-details">
          <dl>
            <dt><%=h city.name %></dt>
            <dd><%=h city.ghome %></dd>
          </dl>
        </td>

        <td class="list-actions">
          <%= link_to 'Show', city %><br />
          <%= link_to 'Edit', edit_city_path(city) %><br />
          <%= link_to 'Destroy', city,
                      :confirm => 'Are you sure?',
                      :method => :delete %></td>
        </td>
      </tr>
    <% end %>
  </table>
</div>


I'm looking at it and if the line <%=h city.name %> works in the first one, why won't it work in the second one?

Last edited by amnion (2010-10-30 21:41:52)

Re: undefined local variable or method

The problem is with this line: <% for cities in @cities %>.

It should be <% for city in @cities %>. I personally would use another iteration style: <% @cities.each do |city| %>.

Re: undefined local variable or method

I don't see how that line got misspelled. I just checked my file and it's "city". Weird. Anyway, I'm actually getting the error from Line 9, according to my app, which is "undefined local variable or method `city' for #<ActionView::Base:0x4bd820c>". 


6:       <tr class="<%= cycle('list-line-odd', 'list-line-even') %>">
7:       
8:         <td>
9:           <%= image_tag city.image_url, :class => 'list-image' %>
10:         </td>
11:         <td class="list-details">
12:           <dl>



## Went ahead and updated the spelling on here.

Last edited by amnion (2010-10-31 04:16:11)

Re: undefined local variable or method

@amnion. Did you get a resolution to this? It is very similar to an error that I am getting.