to_sym not working in erb loop

36 views
Skip to first unread message

gvim

unread,
May 15, 2014, 11:03:38 AM5/15/14
to rubyonra...@googlegroups.com
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' }

........ <other regions> ..........

} %>

<% @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 %>

mike2r

unread,
May 18, 2014, 12:05:16 PM5/18/14
to rubyonra...@googlegroups.com
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.   

Matt Jones

unread,
May 18, 2014, 4:14:42 PM5/18/14
to rubyonra...@googlegroups.com


On Thursday, 15 May 2014 11:03:38 UTC-4, gvim wrote:
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)

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 Jones
Reply all
Reply to author
Forward
0 new messages