2012-02-01 56 views
-1

我嘗試使用表單字段這裏的外部網站創建一個HTTP POST請求是factual.com如何使用Ruby on Rails將表單字段創建到外部網站API的HTTP發佈請求?

我已經創造了我的data_controller.rb這就好比

def posttofactual 

uri = URI.parse("http://api.factual.com/v2/tables/Nj0JN3/input?") 

# Full control 
http = Net::HTTP.new(uri.host, uri.port) 
request = Net::HTTP::Post.new(uri.request_uri) 
request.set_form_data({"name" => "datum.name", "address" => "datum.address", "locality" => "datum.locality", "region" => "datum.region", "postcode" => "datum.postcode","category" => "datum.category","website" => "datum.website","latitude" => "datum.latitude","longitude" => "datum.longitude","&APIKey=" => "myapikey" }) 

# Tweak headers, removing this will default to application/x-www-form-urlencoded 
request["Content-Type"] = "application/json" 

response = http.request(request) 

end 

方法我有這樣

<%= form_for(@datum) do |f| %> 
<% if @datum.errors.any? %> 
<div id="error_explanation"> 
<h2><%= pluralize(@datum.errors.count, "error") %> prohibited this datum from being saved:</h2> 
<ul> 
<% @datum.errors.full_messages.each do |msg| %> 
<li><%= msg %></li> 
<% end %> 
</ul> 
</div> 
<% end %> 
<div class="field"> 
<%= f.label :name %><br /> 
<%= f.text_field :name %> 
</div> 
<div class="field"> 
<%= f.label :address %><br /> 
<%= f.text_field :address %> 
</div> 
<div class="field"> 
<%= f.label :locality %><br /> 
<%= f.text_field :locality %> 
</div> 
<div class="field"> 
<%= f.label :region %><br /> 
<%= f.text_field :region %> 
</div> 
<div class="field"> 
<%= f.label :postcode %><br /> 
<%= f.number_field :postcode %> 
</div> 
<div class="field"> 
<%= f.label :category %><br /> 
<%= f.text_field :category %> 
</div> 
<div class="field"> 
<%= f.label :website %><br /> 
<%= f.text_field :website %> 
</div> 
<div class="field"> 
<%= f.label :latitude %><br /> 
<%= f.text_field :latitude %> 
</div> 
<div class="field"> 
<%= f.label :longitude %><br /> 
<%= f.text_field :longitude %> 
</div> 
<div class="actions"> 
<%= f.submit %> 
</div> 
<% end %>  

我想打電話「posttofactual」方法在這條線上<%= f.submit%>上的表單字段的表單。

+0

塞爾吉奧Tulentsev,我有一個疑問,我需要一個答案,告訴我什麼是我的背景是...如果你考慮幫助這個問題,那麼它的好,否則我會自己找到它..謝謝 – user1166925 2012-02-01 09:47:39

回答

0

看起來你的表格會發布到createupdate控制器的方法。在那裏你應該從params提取數據,並打電話給你的posttofactual方法。