2016-07-12 5 views
0
I라는 보석을 사용하고

건물 검색 REST API를

gem 'open_taobao' 내가 내보기에서 수행하려고하는 것은 입력 필드가 있습니다 내가 json

class SearchController < ApplicationController 
    layout 'layouts/frontend_base' 

    def index 
    @searchterm = "basketball" 
    @search = OpenTaobao.get(
     :method => "taobao.tbk.item.get", 
     :fields => "num_iid,title,nick,pict_url,cid,price,type,delist_time,post_fee,score,volume", 
     :q => @searchterm, 
     :page_size => 1, 
     :sort => "_des", 
    ) 
    end 
end 

내 데이터를 얻을 방법입니다 다음 에 제출하고 어떤 입력 상자에

:q => @searchterm

아래 내 쿼리 변수를 대체 할 일 것입니다 무엇 이 일을 수행하는 가장 좋은 방법은?

답변

1

이 같은 양식을 가질 수 있습니다

def index 
    @search = OpenTaobao.get(
     :method => "taobao.tbk.item.get", 
     :fields => "num_iid,title,nick,pict_url,cid,price,type,delist_time,post_fee,score,volume", 
     :q => params[:q], 
     :page_size => 1, 
     :sort => "_des", 
    ) 
    # what you do depends on the data type returned here. if it's a JSON string, you should parse it and reassign to @search, if not you may not need to do anything 
    end 

에서 :

컨트롤러에
<%= form_tag search_path do %> 
    <%= input_field_tag :q, params[:q] %> 
    <%= submit_tag "Submit" %> 
<% end %> 

, 당신은 @searchterm

당신이 할 수있는 변수 다른 인스턴스를 설정할 필요가 없습니다 보기,

#search/index.html.erb 
<% @search.each do |result_key, result_value| %> 
    # you can deal with the html here 
    <%= result_key %> 
    <%= result_value %> 
<% end %> 
+0

이것은 결국 나에게 잘 작동했다. :) – NooBskie