user2305753
user2305753

Reputation: 15

Passing variables in RoR from html to model

I'm new to developing in Ruby on Rails and I'm stuck on a little project that I've been working on to understand RoR better. I am trying to make a little weather website and I'm having trouble sending user input to a model through a controller and to have that model use send back the correct information so that I can parse it and what not. I have not been able so far to send the user param along to the controller so that it will send out the right request. Here is my following code:

hourly.html.erb:

 <%= 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 %>

hourly_lookup_controller.rb:

class HourlyLookupController < ApplicationController

    def show
        @hourly_lookup = HourlyLookup.new(params[:location])
    end
end

hourly_lookup.rb:

class HourlyLookup

    def fetch_weather
        HTTParty.get("http://api.wunderground.com/api/api-key/hourly/q/CA/#{:location}.xml")
    end

    def initialize
      weather_hash = fetch_weather
      assign_values(weather_hash)
    end

    def assign_values(weather_hash)

       more code....

    end
end

Any help or directions to some good examples or tutorials would be greatly appreciated. Thanks

Upvotes: 1

Views: 70

Answers (1)

Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

If you want to send a variable to HourlyLookup, you'll need to do so:

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

Upvotes: 1

Related Questions