Ruby on rails 将RoR中的变量从html传递到模型
我刚开始使用RubyonRails进行开发,我一直在做一个小项目,以便更好地理解RoR。我正在尝试制作一个小型天气网站,但我很难通过控制器将用户输入发送到模型,并让模型用户发回正确的信息,以便我能够解析它,或者什么都不能。到目前为止,我还无法将用户参数发送到控制器,以便它发送正确的请求。以下是我的代码: hourly.html.erb:Ruby on rails 将RoR中的变量从html传递到模型,ruby-on-rails,ruby,Ruby On Rails,Ruby,我刚开始使用RubyonRails进行开发,我一直在做一个小项目,以便更好地理解RoR。我正在尝试制作一个小型天气网站,但我很难通过控制器将用户输入发送到模型,并让模型用户发回正确的信息,以便我能够解析它,或者什么都不能。到目前为止,我还无法将用户参数发送到控制器,以便它发送正确的请求。以下是我的代码: hourly.html.erb: <%= form_tag('/show') do %> <%= label_tag(:location, "Enter in
<%= form_tag('/show') do %>
<%= label_tag(:location, "Enter in your city name:") %>
<%= text_field_tag(:location) %>
<br />
<%= submit_tag "Check It Out!", class: 'btn btn-primary' %>
<% end %>
如果您对一些好的示例或教程有任何帮助或指导,我们将不胜感激。谢谢如果要向HourlyLookup发送变量,您需要执行以下操作:
class HourlyLookupController < ApplicationController
def show
@hourly_lookup = HourlyLookup.new(params[:location])
@hourly_lookup.fetch_weather
end
end
class HourlyLookup
attr_reader :location
def initialize(location)
@location = location
end
def fetch_weather
response = HTTParty.get("http://api.wunderground.com/api/cdb75d07a23ad227/hourly/q/CA/#{location}.xml")
parse_response(response)
end
def parse_response(response)
#parse the things
end
end
class HourlyLookupController
如果要向HourlyLookup发送变量,需要执行以下操作:
class HourlyLookupController < ApplicationController
def show
@hourly_lookup = HourlyLookup.new(params[:location])
@hourly_lookup.fetch_weather
end
end
class HourlyLookup
attr_reader :location
def initialize(location)
@location = location
end
def fetch_weather
response = HTTParty.get("http://api.wunderground.com/api/cdb75d07a23ad227/hourly/q/CA/#{location}.xml")
parse_response(response)
end
def parse_response(response)
#parse the things
end
end
class HourlyLookupController
太棒了,它能工作!非常感谢您快速正确的回复!到目前为止,Rails非常酷,但有点令人困惑。再次感谢!太棒了,它工作了!非常感谢您快速正确的回复!到目前为止,Rails非常酷,但有点令人困惑。再次感谢!
class HourlyLookupController < ApplicationController
def show
@hourly_lookup = HourlyLookup.new(params[:location])
@hourly_lookup.fetch_weather
end
end
class HourlyLookup
attr_reader :location
def initialize(location)
@location = location
end
def fetch_weather
response = HTTParty.get("http://api.wunderground.com/api/cdb75d07a23ad227/hourly/q/CA/#{location}.xml")
parse_response(response)
end
def parse_response(response)
#parse the things
end
end