通过 RoR 发布并返回 JSON,现在怎么办?
好的,我对 Ruby on Rails 完全陌生。我创建了一个表单,用于发布到返回 JSON 的外部小部件。所以我有这样的形式:
<%= form_for :email, :url => 'http://XXX.XX.XXX.212/widgetapi.0.1.php', :html => {:class => "new_email"} do |f| %>
<%= f.text_field :email, :value => "Your email address...", :class => "text", :id => "email", :name => 'email',
:onFocus => "change(this,'#222222'); this.value=''; this.onfocus=null;",
:size => "26" %>
<%= f.hidden_field :apiKey, :id => "apiKey", :name => 'apiKey', :value => "ABC123" %>
<%= f.hidden_field :lrDomain, :id => "lrDomain", :name => 'lrDomain', :value => "signup.triplingo.com" %>
<%= f.hidden_field :urlPrefix, :id => "refCodeUrl", :name => 'refCodeUrl', :value => "http://signup.website.com/" %>
<%= f.hidden_field :ref_code, :id => 'ref_code', :name => 'ref_code', :value => @referralid %>
<%= submit_tag "Enter To Win", :class => "button-positive submit" %>
<% end %>
哪个有效。现在我得到一个 JSON 响应,即:
({"email":"[email protected]","reflink":"fi1ts","newuser":true})
好的,现在浏览器使用 JSON 位于响应页面上的结果。
我猜我需要对控制器中的 @response 做一些事情,但我不确定做什么。我想做的就是如果“newuser”是真的,为他们提供一个成功页面。如果为 false,则转至错误页面。
谢谢。
OK, I'm totally new to Ruby on Rails. I created a form that posts to an external widget that returns JSON. So I have this form:
<%= form_for :email, :url => 'http://XXX.XX.XXX.212/widgetapi.0.1.php', :html => {:class => "new_email"} do |f| %>
<%= f.text_field :email, :value => "Your email address...", :class => "text", :id => "email", :name => 'email',
:onFocus => "change(this,'#222222'); this.value=''; this.onfocus=null;",
:size => "26" %>
<%= f.hidden_field :apiKey, :id => "apiKey", :name => 'apiKey', :value => "ABC123" %>
<%= f.hidden_field :lrDomain, :id => "lrDomain", :name => 'lrDomain', :value => "signup.triplingo.com" %>
<%= f.hidden_field :urlPrefix, :id => "refCodeUrl", :name => 'refCodeUrl', :value => "http://signup.website.com/" %>
<%= f.hidden_field :ref_code, :id => 'ref_code', :name => 'ref_code', :value => @referralid %>
<%= submit_tag "Enter To Win", :class => "button-positive submit" %>
<% end %>
Which works. Now I get back a JSON response that is:
({"email":"[email protected]","reflink":"fi1ts","newuser":true})
Ok now the result it the browser sits on the response page with the JSON.
I'm guessing I need to do something with the @response in the controller, but I'm not sure what. All I want to do is if that "newuser" is true, provide them a success page. If false, go to an error page.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该更改该表单,以便它将请求发送到您的控制器。在该控制器中,您应该使用表单中的参数进行 API 调用(例如使用 Curb: https://github.com/ taf2/curb 或 Net/http:http ://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html)。然后您可以解析 JSON 格式的结果并向用户显示正确的页面。
因此:
1)用户向您的应用程序发送请求
2)您的应用程序向 http:// 发出请求XXX.XX.XXX.212/widgetapi.0.1.php 使用来自用户的数据
3) 您的应用程序接收 JSON,并检查 newuser 是否为 true。
4) 如果为true则action渲染成功页面,否则渲染错误页面。
You should change that form, so that it will send request to your controller. In that controller you should do API Call with parameters from a form(using for example Curb: https://github.com/taf2/curb or Net/http: http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html). Then You can parse result in JSON and show user correct page.
So:
1) User send request to YOUR application
2) YOUR application make request to http://XXX.XX.XXX.212/widgetapi.0.1.php using data from user
3) YOUR application receives JSON, and check if newuser is true.
4) If it is true action render success page, otherwise it render error page.