0

我想这样做:

In the view, have a button_to "Find"
The button click submits the search query to the rails action (controller)
The action calls some external api
A response comes back in 1ms up to 20secs (who knows? it's an external webservice, right?)
When the response is returned, the view which originally contains the button_to and the search field is updated with the response (via javascript)
  1. 这背后的基本思想是什么?
  2. 这叫什么?
  3. 返回响应时如何更新视图?

谢谢

4

1 回答 1

1
  1. 当您想更改发出请求的页面时,您的按钮将触发 AJAX 调用。在服务器端,您只需要使用像HTTParty这样的库来进行调用,等待响应到达服务器,根据需要进行处理,然后自己发送响应。

  2. AFAIK 这个动作没有特定的名称;但是我们可以说您的服务器充当另一个服务的代理,如果您重新处理来自许多不同 API 的答案,那么您的服务就是一种聚合器

  3. 要更新视图,这取决于您如何实现 ajax 调用。您可以:

    • 使用 JSON 数据响应一些,您的回调将处理并用于更新页面,可能使用像把手这样的模板系统
    • 用一些 JS 响应,通常是一个将在客户端上执行的片段,例如

      $('#my_element').replaceWith('<%= j( render partial: "some_partial" ) %>');

于 2013-02-28T18:57:49.703 回答