响应进入时更新rails视图

时间:2013-02-28 18:33:22

标签: javascript ruby-on-rails ruby ajax api

我想这样做:

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 个答案:

答案 0 :(得分:1)

  1. 当您想要更改发出请求的页面时,您的按钮将触发AJAX调用。在服务器端,您只需使用类似HTTParty的lib来拨打电话,等待响应服务器,根据需要进行处理,然后自行发送响应。

  2. AFAIK此操作没有具体名称;但是我们可以说你的服务器充当另一个服务的代理,如果你从许多不同的API重新处理答案,那么你的服务就是一种聚合器 < / p>

  3. 要更新视图,具体取决于您实施ajax调用的方式。你可以:

    • 回复一些JSON数据,您的回调将处理并用于更新页面,可能使用handlebars等模板系统
    • 回复一些JS,通常是一个会在客户端执行的代码片段,如

      $('#my_element')。replaceWith('&lt;%= j(render partial:“some_partial”)%&gt;');