Ruby on Rails: Starting Twitter Gem after page loading

I use the 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.

Currently, the user will need to wait until the page is displayed, as the pearl of Twitter is working on requesting some tweets. After Twitter receives a response, the page loads.

However, I was hoping to display the “loading” gif in the “div” tag of Twitter, and after receiving the information on Twitter, replace this gif with the appropriate content.

I read a few posts on how to use the form and the :: = = true tag on the form to make AJAX calls that won’t "reload" the corresponding page, but I don’t know if you want to force visitors to click the link to see Twitter tweets .

I am very green to chop rails, so any feedback would be greatly appreciated. Thanks!!

Ruby version: 1.8.7 Rails version: 3.0.9

+3
source share
1 answer

There are several ways to do this; A fairly common practice and the way I will deal with this is to use jQuery $ .ajax () to get data from the server and refresh the page.

on the client side:

  • Load the page normally and show some loading indicator.

  • Make a call to $ .ajax ():

    $.ajax({ type: "GET", url: userId + '/tweets', // ie 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'>); }); } }); 

server side:

Have an action in the controller that sends tweets as JSON that will be consumed by the client side callback. Configure routing so that this action is compatible with the URL in $ .ajax ()

 class TweetsController < ApplicationController def show # collect tweets... render :json => @tweets end end 
+4
source

Source: https://habr.com/ru/post/1339284/


All Articles