简体   繁体   English

Ruby on Rails控制器(新手)

[英]Ruby on Rails controller (newbie)

Below is very simple Ruby code that gets the weather. 下面是获取天气的非常简单的Ruby代码。 I'm trying Rails for the first time and am having a hard time adding the ruby code to my pages_controller.rb 我是第一次尝试Rails,很难将ruby代码添加到我的pages_controller.rb

My question is how to best add the code into my controller. 我的问题是如何最好地将代码添加到控制器中。

I haven't seen any tutorials that breaks this down simply. 我还没有看过任何可以简单地将其分解的教程。 Any help/examples are appreciated. 任何帮助/示例,不胜感激。

def your_destination
    puts "Where are you going?"
    @trip_destination = gets.chomp
    HTTParty.get("http://api.wunderground.com/api/#######key######/geolookup/conditions/q/#{@trip_destination}.json")
end 

def weather_values(your_destination)
    current_temp = your_destination.parsed_response['current_observation']['temp_f'].to_s
    current_condition = your_destination.parsed_response['current_observation']['weather'].to_s
    puts "It is " + current_temp + " and " + current_condition + "!"
end

weather_values(your_destination)

This isn't rails. 这不是铁轨。 This is plain ruby. 这是红宝石。 Try railstutorial.org. 尝试railstutorial.org。 they have an excellent eBook you can read online for free 他们有出色的电子书,您可以免费在线阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM