Forum: Ruby can't convert Hash into String

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9c3ca68871c5e2218e8e4a0d73f6b684?d=identicon&s=25 Rajat Garg (rajatgarg)
on 2008-11-21 06:46
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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-11-21 09:52
> 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.
9c3ca68871c5e2218e8e4a0d73f6b684?d=identicon&s=25 Rajat Garg (rajatgarg)
on 2008-11-21 19:37
Brian,

Whereever postal code is empty, it fails -
    <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>


Here is the XML that works -
  <Hotel>^M
    <hotelId>117451</hotelId>^M
    <name>Days Inn - Seattle Downtown</name>^M
    <address1>2205 7th Ave</address1>^M
    <address2/>^M
    <address3/>^M
    <city>SEATTLE</city>^M
    <stateProvince>WA</stateProvince>^M
    <country>US</country>^M
    <postalCode>98121</postalCode>^M
    <airportCode>SEA</airportCode>^M
    <lowRate>79.0</lowRate>^M
    <highRate>89.0</highRate>^M
    <rateCurrencyCode>USD</rateCurrencyCode>^M
    <latitude>47.61671</latitude>^M
    <longitude>-122.34011</longitude>^M
    <shortDescription>&amp;lt;b&amp;gt;Location.&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;
&amp;lt;ul&amp;gt; &amp;lt;li&amp;gt;The Days Inn - Seattle Downtown is
located in Seattle, Wash.</shortDescription>^M
    <thumbNailUrl>/hotels/thumbs/SEA_DITC-exter-1-thumb.jpg</thumbNailUrl>^M
    <supplierType>H</supplierType>^M
    <location>DOWNTOWN</location>^M
    <propertyRating>2.0</propertyRating>^M
    <propertyType>1</propertyType>^M
    <marketingLevel>1</marketingLevel>^M
    <hasMap>true</hasMap>^M
    <hotelInDestination>true</hotelInDestination>^M
    <referenceProximityDistance>11.467874</referenceProximityDistance>^M
    <referenceProximityUnit>MI</referenceProximityUnit>^M
    <HotelProperty>^M
      <specialRate>N</specialRate>^M
      <promoDescription>Fall &amp;amp; Holiday
Sale!</promoDescription>^M
      <promoType/>^M
      <promoDetailText/>^M
      <ValueAdds>^M
        <valueAdd id='2048'>Free Wireless Internet</valueAdd>^M
        <valueAdd id='2'>Continental Breakfast</valueAdd>^M
      </ValueAdds>^M
      <hrnQuoteKey>098A66135610001269</hrnQuoteKey>^M
      <currentAllotment>-1</currentAllotment>^M
      <propertyId>16017</propertyId>^M
      <propertyAvailable>true</propertyAvailable>^M
      <propertyRestricted>false</propertyRestricted>^M
      <roomDescription>Standard room</roomDescription>^M
      <roomTypeCode>107441</roomTypeCode>^M
      <rateCode>247324</rateCode>^M
      <RateInfo>^M
        <displayCurrencyCode>USD</displayCurrencyCode>^M
        <DisplayNightlyRates size='2'>^M
          <displayNightlyRate>79.0</displayNightlyRate>^M
          <displayNightlyRate>89.0</displayNightlyRate>^M
        </DisplayNightlyRates>^M
        <displayRoomRate>197.01</displayRoomRate>^M
        <chargeableRoomRateTotal>197.01</chargeableRoomRateTotal>^M
        <chargeableRoomRateTaxesAndFees>29.01</chargeableRoomRateTaxesAndFees>^M
        <nativeCurrencyCode>USD</nativeCurrencyCode>^M
        <NativeNightlyRates size='2'>^M
          <nativeNightlyRate>79.0</nativeNightlyRate>^M
          <nativeNightlyRate>89.0</nativeNightlyRate>^M
        </NativeNightlyRates>^M
        <nativeRoomRate>197.01</nativeRoomRate>^M
        <rateFrequency>B</rateFrequency>^M
      </RateInfo>^M
      <PromoRateInfo>^M
        <displayCurrencyCode>USD</displayCurrencyCode>^M
        <DisplayNightlyRates size='2'>^M
          <displayNightlyRate>79.0</displayNightlyRate>^M
          <displayNightlyRate>89.0</displayNightlyRate>^M
        </DisplayNightlyRates>^M
        <displayRoomRate>197.01</displayRoomRate>^M
        <chargeableRoomRateTotal>197.01</chargeableRoomRateTotal>^M
        <chargeableRoomRateTaxesAndFees>29.01</chargeableRoomRateTaxesAndFees>^M
        <nativeCurrencyCode>USD</nativeCurrencyCode>^M
        <NativeNightlyRates size='2'>^M
          <nativeNightlyRate>79.0</nativeNightlyRate>^M
          <nativeNightlyRate>89.0</nativeNightlyRate>^M
        </NativeNightlyRates>^M
        <nativeRoomRate>197.01</nativeRoomRate>^M
        <rateFrequency>B</rateFrequency>^M
      </PromoRateInfo>^M
    </HotelProperty>^M
  </Hotel>^M
This topic is locked and can not be replied to.