To_sym not working in erb loop

I’m trying to generate rows in a HTML table in my view (see below) but:

zone[loc.to_sym]

… seems to be nil as I keep getting the error :

undefined method `each’ for nil:NilClass for this line

gvim

************* Controller ********************
class MainController < ApplicationController
def home
@locations = %w(Africa America Arctic Asia Atlantic Australia
Europe Brazil Canada Chile Indian Mexico Pacific US Misc)
end

****************** View *********************
<% zones = {
US: { ‘US/Alaska’ => ‘Alaska’, ‘US/Aleutian’ => ‘Aleutian’, ‘US/Arizona’
=> ‘Arizona’, ‘US/Central’ => ‘Central’, ‘US/East-Indiana’ => ‘East
Indiana’, ‘US/Eastern’ => ‘Eastern’, ‘US/Hawaii’ => ‘Hawaii’,
‘US/Indiana-Starke’ => ‘Indiana Starke’, ‘US/Michigan’ => ‘Michigan’,
‘US/Mountain’ => ‘Mountain’, ‘US/Pacific’ => ‘Pacific’, ‘US/Pacific-New’
=> ‘Pacific New’, ‘US/Samoa’ => ‘Samoa’ },

Miscellaneous: { Cuba:‘Cuba’, Egypt:‘Egypt’, Eire:‘Eire’,
Greenwich:‘Greenwich’, Hongkong:‘Hongkong’, Iceland:‘Iceland’,
Iran:‘Iran’, Israel:‘Israel’, Jamaica:‘Jamaica’, Japan:‘Japan’,
Kwajalein:‘Kwajalein’, Libya:‘Libya’, Navajo:‘Navajo’, Poland:‘Poland’,
Portugal:‘Portugal’, Singapore:‘Singapore’, Turkey:‘Turkey’, Zulu:‘Zulu’
}

… …

} %>

   <% @locations.each do |loc| %>
   <tr>
     <td align="right">&nbsp;</td>
       <td><select name="<%= loc %>1">
           <option value="" selected="selected"><%= loc %></option>
           <% zones[loc.to_sym].each do |key, val| %>
           <option value="<%= key %>"><%= val %></option>
           <% end %>
         </select>
     </td>
     <td align="right">&nbsp;</td><td></td>
   </tr>
   <% end %>

On Thursday, May 15, 2014 11:03:38 AM UTC-4, gvim wrote:

US: { ‘US/Alaska’ => ‘Alaska’, ‘US/Aleutian’ => ‘Aleutian’, ‘US/Arizona’
Portugal:‘Portugal’, Singapore:‘Singapore’, Turkey:‘Turkey’, Zulu:‘Zulu’ }
<% zones[loc.to_sym].each do |key, val| %>
<%= val %>
<% end %>


 

<% end %>

actually, to_sym is probably working and looks like it should. You’re
going to have an issue here where there is an element defined in
locations
but not in zones. In your example, there is location Arctic. In your
table code, that’s going to evaluate to:

zones[:Arctic].each do |key, val|

If there is no Arctic defined in zones, zones[:Arctic] will evaluate to
nil, giving you the message you got above.

On Thursday, 15 May 2014 11:03:38 UTC-4, gvim wrote:

************* Controller ********************
class MainController < ApplicationController
def home
@locations = %w(Africa America Arctic Asia Atlantic Australia
Europe Brazil Canada Chile Indian Mexico Pacific US Misc)

Not sure if this is a harmless copy-paste error or a significant one,
but
you’ve got ‘Misc’ here and ‘Miscellaneous’ below. I’d also be a bit
suspicious about “Indian”, since the rest of the locations appear to be
either continents or countries…

To investigate this further, I’d recommend either breaking into the loop
with the debugger of your choice or logging the value of location each
time around.

–Matt J.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs