Ruby on Rails的:运行Twitter的宝石后,页面加载加载、页面、on、Ruby

2023-09-10 17:42:06 作者:被自己蠢哭了

我使用的是Twitter的宝石(http://twitter.rubyforge.org/),和它的伟大工程。不过,我希望了解如何在后台运行,在页面加载后。

I am using a Twitter gem (http://twitter.rubyforge.org/), and it works great. However, I was hoping to understand how to "run it in the background," after the page loads.

目前,用户需要等待页面来呈现,因为Twitter的创业板正在查询一些鸣叫。一旦叽叽喳喳的宝石得到答复,页面加载。

Currently the user would need to wait for the page to render, since the Twitter gem is working to query some tweets. Once the twitter gem gets its response, the page loads.

不过,我希望能在我的Twitter的饲料分区标签显示加载GIF,一旦微博信息的检索,替换这个GIF与相应的内容。

However, I was hoping to display a "loading" gif in my twitter feed "div" tag, and once the twitter information is retrieved, replace this gif with the respective content.

我读过关于如何使用的一种形式几个职位和:远程=>真正的表单上的标签,为了做到这一点不会重新载入各页的AJAX调用,但我不吨要强迫访问者点击一个链接,才能看到Twitter的鸣叫。

I've read several posts on how to use a form and the ":remote => true" tag on the form, in order to do AJAX calls that won't "reload" the respective page, but I don't want to force visitors to click a link, in order to see Twitter tweets.

我很绿色,以Ruby on Rails的,所以任何反馈意见,将大大AP preciated。谢谢!

I'm very green to ruby on rails, so any feedback would be greatly appreciated. Thanks!!

红宝石版本:1.8.7 Rails的版本:3.0.9

Ruby version: 1.8.7 Rails version: 3.0.9

推荐答案

有这样做的多种方法;一个相当普遍的做法和方式,我会去,这是使用了jQuery的的 $。阿贾克斯()的,以从服务器获取数据,并更新页面。

There are multiple ways of doing this; A fairly common practice and the way I would go about this is to use the the jQuery's $.ajax() to get data from the server and update the page.

正常加载页面,并显示出某种负荷指标。

Load the page normally and show some kind of loading indicator.

请调用$阿贾克斯():

Make a call to $.ajax():

$.ajax({
  type: "GET",
  url: userId + '/tweets', // i.e. tweets is a nested resource of user
  success: function(tweets){
    $('#loading_indicator').hide();

    // Use the tweets that are returned to update the DOM
    $.each(tweets, function(ndx, tweet) {
      $('#tweet_list').append('<li>'+tweet.body+<'/li'>);
    });
  }
});

有在发出鸣叫的JSON由客户端的成功的回调消耗控制器的操作。设置路由的这一行动是兼容的网址的在$。阿贾克斯()

Server-side:

Have an action in a controller that sends out tweets as JSON to be consumed by the client-side success callback. Setup the routing for this action to be compatible with url in $.ajax()

class TweetsController < ApplicationController

  def show
    # collect tweets...
    render :json => @tweets
  end

end