2013-04-22 2 views
1

Ruby on Rails를 처음 사용하고 RoR을 더 잘 이해하기 위해 노력한 작은 프로젝트에 착수했습니다. 조금 날씨 웹 사이트를 만들려고하고 컨트롤러를 통해 모델에 사용자 입력을 보내고 해당 모델을 사용하여 올바른 정보를 보내면 구문 ​​분석 할 수 있고 무엇이 아닌지 알 수 있습니다. 필자는 지금까지 사용자 매개 변수를 컨트롤러에 보내서 올바른 요청을 보낼 수 없었습니다. 여기 내 다음 코드 :RoR의 변수를 html에서 모델로 전달

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 

어떤 도움이나 방향에 좋은 예제 또는 튜토리얼을 크게 감상 할 수 있습니다. 당신이 HourlyLookup에 변수를 보내려면 감사

답변

1

것은, 당신은 그렇게해야합니다 :

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 
+0

이 굉장 작동! 신속하고 올바른 답변을 해주셔서 대단히 감사합니다! Rails는 지금까지는 꽤 멋졌지만 다소 혼란 스럽습니다. 다시 한 번 감사드립니다! – user2305753

관련 문제