2016-07-12 2 views
0

以下は、私が私の見解からやろうとしていますどのような私はjsonビルサーチのREST API

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 

に私のデータを取得する方法であるgem 'open_taobao'

と呼ばれる入力フィールドを持っています提出し、どのような入力ボックスにある

:q => @searchterm

の下に私のクエリ変数を置換するだろう何これを達成する最良の方法は?あなたが行うことができ、あなたのコントローラで

<%= form_tag search_path do %> 
    <%= input_field_tag :q, params[:q] %> 
    <%= submit_tag "Submit" %> 
<% end %> 

、あなたは別のインスタンスを設定する必要はありません変数@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 

であなたの見解、

#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