Can't convert Hash into String


#1

Hi,

I get an XML like this from a hotel API -

<name>Oriental Riverside Hotel - Pudong Shanghai</name>
<address1>No. 2727, Riverside Ave., Pu</address1>
<address2/>
<address3/>
<city>SHANGHAI</city>
<stateProvince/>
<country>CN</country>
<postalCode> </postalCode>
<airportCode>SHA</airportCode>

in rhtml page, I get an error -

ActionView::TemplateError (can’t convert Hash into String) on line #36
of app/views/hotels2/city_hotels.rhtml:
33: address += ’ ’
34:
35: if hotel[‘postalCode’][0].is_a?(Hash)
36: address += hotel[‘postalCode’][0]
37: end
38:
39: description = hotel[‘shortDescription’][0]

It is not able to interpret empty postal code tag correctly. How should
I handle it?

Thanks

/r
http://www.pilotoutlook.com


#2

33: address += ’ ’

It looks like address is a String

35: if hotel[‘postalCode’][0].is_a?(Hash)

You are checking explicitly that hotel[‘postalCode’][0] is indeed a Hash

36: address += hotel[‘postalCode’][0]

You are trying to do string + hash, which doesn’t make sense (and that’s
what Ruby is telling you)

Have a look at what is actually being sent to you, i.e. the contents of
the hash, using hotel.inspect

Perhaps you if hotel[…][…].is_a?(String) earlier on. Or perhaps you
need another level of deferenecing, e.g. hotel[…][…][…] to get to
the value you want. Without seeing the contents of the ‘hotel’ object, I
can’t say.


#3

Brian,

Whereever postal code is empty, it fails -
Oriental Riverside Hotel - Pudong Shanghai
No. 2727, Riverside Ave., Pu


SHANGHAI

CN

SHA

Here is the XML that works -
^M
117451^M
Days Inn - Seattle Downtown^M
2205 7th Ave^M
^M
^M
SEATTLE^M
WA^M
US^M
98121^M
SEA^M
79.0^M
89.0^M
USD^M
47.61671^M
-122.34011^M
&lt;b&gt;Location.&lt;/b&gt;&lt;br&gt;
&lt;ul&gt; &lt;li&gt;The Days Inn - Seattle Downtown is
located in Seattle, Wash.^M
/hotels/thumbs/SEA_DITC-exter-1-thumb.jpg^M
H^M
DOWNTOWN^M
2.0^M
1^M
1^M
true^M
true^M
11.467874^M
MI^M
^M
N^M
Fall &amp; Holiday
Sale!^M
^M
^M
^M
Free Wireless Internet^M
Continental Breakfast^M
^M
098A66135610001269^M
-1^M
16017^M
true^M
false^M
Standard room^M
107441^M
247324^M
^M
USD^M
^M
79.0^M
89.0^M
^M
197.01^M
197.01^M
29.01^M
USD^M
^M
79.0^M
89.0^M
^M
197.01^M
B^M
^M
^M
USD^M
^M
79.0^M
89.0^M
^M
197.01^M
197.01^M
29.01^M
USD^M
^M
79.0^M
89.0^M
^M
197.01^M
B^M
^M
^M
^M