Ruby on rails 如何使用RubyonRails,使用外部网站API的表单字段创建HTTP post请求?

Ruby on rails 如何使用RubyonRails,使用外部网站API的表单字段创建HTTP post请求?,ruby-on-rails,ruby-on-rails-3,api,post,activeresource,Ruby On Rails,Ruby On Rails 3,Api,Post,Activeresource,我正在尝试使用表单字段创建一个HTTP post请求到外部网站这里是factual.com 我在my 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(

我正在尝试使用表单字段创建一个HTTP post请求到外部网站这里是factual.com

我在my 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 %>    

禁止保存此数据:










我想在表单字段的这一行调用“PostToFact”方法。

您的表单似乎将发布到控制器的
create
update
方法。你应该从
params
中提取数据,然后调用你的
posttofact
方法。

Sergio Tulentsev,我有一个疑问,我需要一个答案,告诉你我的背景是什么。。。如果你考虑帮助这个问题,那就好了,否则我会自己找到的。谢谢