我正在尝试使用表单字段创建一个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 %>
我想在这一行上调用“posttofactual”方法&lt;%= f.submit%&gt;在表格领域。
答案 0 :(得分:0)
您的表单似乎会发布到控制器的create
或update
方法。在那里,您应该从params
中提取数据并调用posttofactual
方法。