Я все еще больше узнаю о Rails, и я начинаю играть с API-интерфейсами, но я не могу понять, как получить входные данные из формы Модель.Ruby on Rails: ввод формы в модель
Я хочу взять пользовательский ввод (в виде почтового индекса) и заставить его выплевывать информацию о погоде в этом местоположении пользователя.
Форма на home.html.erb
<%= form_tag(root_path) do %>
<%= label_tag :zip, "ENTER YOUR ZIPCODE TO FIND YOUR WEATHER" %><br>
<%= text_field_tag :zip,'', placeholder: "e.g. 91765 " %>
<%= submit_tag "show me the weather!" %>
<% end %>
контроллер pages_controller.rb
class PagesController < ApplicationController
def home
@weather_lookup = WeatherLookup.new(params[:zip])
end
end
Модель weather_lookup.rb
class WeatherLookup
attr_accessor :temperature, :weather_condition, :city, :state, :zip
def initialize(zip)
self.zip = zip
zip = 91765 if zip.blank?
weather_hash = fetch_weather(zip)
weather_values(weather_hash)
end
def fetch_weather(zip)
p zip
HTTParty.get("http://api.wunderground.com/api/API-KEY-HERE/geolookup/conditions/q/#{zip}.json")
end
def weather_values(weather_hash)
self.temperature = weather_hash.parsed_response['current_observation']['temp_f']
self.weather_condition = weather_hash.parsed_response['current_observation']['weather']
self.city = weather_hash.parsed_response['location']['city']
self.state = weather_hash.parsed_response['location']['state']
end
end
Я точно не знаю, как получить информацию от формы к модели. Это буквально просто для того, чтобы показать погоду. Я ничего не пытаюсь сохранить в базе данных.
спасибо миллион! это делает именно то, что я хочу сделать сейчас! – huihuihui