2012-09-09 20 views
0

我有這樣的代碼:不能存取權限乘法項目XML API wunderground

型號:

class WeatherLookup 
    attr_accessor :temperature, :icon, :condition, :zip, :fcttext 

    def fetch_weather(city) 
     HTTParty.get("http://api.wunderground.com/api/api_key/forecast/lang:NL/q/IT/#{city.slug}.xml") 
    end 

    def initialize 
     weather_hash = fetch_weather 
    end 

    def assign_values(weather_hash) 
     hourly_forecast_response = weather_hash.parsed_response['response']['forecast']['txt_forecast']['forecastdays']['forecastday'].first 
     self.fcttext = hourly_forecast_response['fcttext'] 
     self.icon = hourly_forecast_response['icon_url'] 

    end 


    def initialize(city) 
    @city = city 
    weather_hash = fetch_weather(city) 
    assign_values(weather_hash) 
    end 

end 

city_controller:

@weather_lookup = WeatherLookup.new(@city) 

city_view:

= @weather_lookup.fcttext 
     = image_tag @weather_lookup.icon 

這做工精細。 ..我得到拳頭數據集預測日期容器。從API的XML看起來是這樣的:

<response> 
<version>0.1</version> 
<termsofService> 
http://www.wunderground.com/weather/api/d/terms.html 
</termsofService> 
<features> 
<feature>forecast</feature> 
</features> 
<forecast> 
<txt_forecast> 
<date>2:00 AM CEST</date> 
<forecastdays> 
<forecastday> 
<period>0</period> 
<icon>clear</icon> 
<icon_url>http://icons-ak.wxug.com/i/c/k/clear.gif</icon_url> 
<title>zondag</title> 
<fcttext> 
<![CDATA[ Helder. Hoog: 86F. Light Wind. ]]> 
</fcttext> 
<fcttext_metric> 
<![CDATA[ Helder. Hoog: 30C. Light Wind. ]]> 
</fcttext_metric> 
<pop>0</pop> 
</forecastday> 
<forecastday> 
<period>1</period> 
<icon>clear</icon> 
<icon_url>http://icons-ak.wxug.com/i/c/k/clear.gif</icon_url> 
<title>zondagnacht</title> 
<fcttext> 
<![CDATA[ Helder. Laag: 61F. Light Wind. ]]> 
</fcttext> 
<fcttext_metric> 
<![CDATA[ Helder. Laag: 16C. Light Wind. ]]> 
</fcttext_metric> 
<pop>0</pop> 
</forecastday> 
<forecastday> 
<period>2</period> 
<icon>partlycloudy</icon> 
<icon_url>http://icons-ak.wxug.com/i/c/k/partlycloudy.gif</icon_url> 
<title>maandag</title> 
<fcttext> 
<![CDATA[ Gedeeltelijk bewolkt. Hoog: 84F. Light Wind. ]]> 
</fcttext> 
<fcttext_metric> 
<![CDATA[ Gedeeltelijk bewolkt. Hoog: 29C. Light Wind. ]]> 
</fcttext_metric> 
<pop>20</pop> 
</forecastday> 
<forecastday> 
<period>3</period> 
<icon>clear</icon> 
<icon_url>http://icons-ak.wxug.com/i/c/k/clear.gif</icon_url> 
<title>maandagnacht</title> 
<fcttext> 
<![CDATA[ Gedeeltelijk bewolkt. Laag: 63F. Light Wind. ]]> 
</fcttext> 
<fcttext_metric> 
<![CDATA[ Gedeeltelijk bewolkt. Laag: 17C. Light Wind. ]]> 
</fcttext_metric> 
<pop>0</pop> 
</forecastday> 

我想通過一個循環存取權限的預測容器中的所有foracasts,但是當我改變hourly_forecast變量(。第一)到。所有或者沒有我得到的錯誤信息「無法將字符串轉換爲整數」

有人想辦法解決這個問題嗎? \

回答

0

一旦從hourly_forecast刪除。首先,你現在返回集合,而不是單個項目。這意味着您的視圖將無法正常工作,因此您需要更新視圖以呈現集合。這是最好的部分。

city_view將變爲:

= render partial: "weather_lookup", collection: @weather_lookup 

然後創建一個名爲局部_weather_lookup.html.haml,包括以下內容:

= @weather_lookup.fcttext 
     = image_tag @weather_lookup.icon 

在部分的文件名前的下劃線一樣重要這是Rails約定,所以不要忽略它。查看時,部分將爲集合中的每個項目呈現多次。

+0

感謝您的回覆。我按照你的建議改變了代碼,但是我仍然得到錯誤信息..'不能將字符串轉換爲整數'我的日誌:app/models/weather_lookup.rb:15:'[]' app/models/weather_lookup .rb:15:在'assign_values'中 app/models/weather_lookup.rb:24:在'initialize' – Remco