1

I am writing my first Rails app using the twitter gem. I'm simply retrieving search results and trying to cycle through them individually every 5 seconds or so.

My thought was to create a variable and have this variable represent the array index and simply update this variable dynamically with Javascript (every 5 seconds or so). What's the best way to achieve this on the client-side? AJAX? Javascript?

Does this make sense? I will be glad to provide more context if helpful. Thanks.

1
  • 1
    What is the goal of incrementing the ruby var every 5 seconds? It sounds like a lot of server calls unless there is a major reason. Commented Oct 3, 2011 at 14:55

4 Answers 4

3

Sounds you're trying to build a "recent tweets" marquee of some sort. Without knowing your requirements, you could try simply loading the ten most recent tweets in Rails, putting them in ten hidden divs, and then using jQuery just to cycle through the different tweets on the page.

If it is a requirement to "update" the most recent tweets without the user refreshing the page, then yes, you'd probably need an AJAX call.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks elliot. i may try out this method. i wasn't even thinking about this technique.
is there a way to scroll through these like its done in www.twistori.com?
2

It's hard to tell what you think you're asking: by the time your JavaScript is executing the server is no longer involved.

If you want to update some sort of count on the server side and persist it in a meaningful way, you can do so via Ajax.

What are you actually trying to do, though?

5 Comments

thanks dave...i think ajax is probably the way to go, i'll look into that now. i'm using the twitter gem to get tweets matching a search query and then updating an array index to cycle through each individually every 5 seconds or so. is ajax the way to go?
Still have no idea what you're actually doing, so it's impossible to say.
Not really. What is the purpose of the variable you want to update? If you're just trying to retrieve tweets every five seconds, use a simple JavaScript timer.
the way the gem works (in my limited understanding) is that it returns an array of, at max, 100 results. i'm sure there is a better way, but i was thinking i would cycle through this array by updating the a variable 0 through 99 to represent the array's index
And why are you cycling through this? If you're just trying to display them after a request, just loop over them in a template (that would also work for an ajax update that returns rendered HTML).
1

Ruby runs on the server while JavaScript (usually) runs on the client.

The Ruby generates an HTML document (perhaps with embedded JS) and the server delivers it to the client.

At that stage the Ruby has finished executing. The only way to do anything further with Ruby would be to make a new HTTP request to the server. This could be done by following a link, submitting a form, setting location.href, using XMLHttpRequest or numerous other techniques.

This would cause the Ruby program to be executed again (or a different one to be executed) which would do whatever it did with the input data.

You cannot simply "set a variable" on the server from the client.

1 Comment

Thanks @Quentin. Understand. I updated my post's content to (hopefully) provide more clarity on what I'm trying to accomplish. I definitely am looking to update the variable on the client side without sending any addition HTTP requests.
0

In my particular case, I used ruby's .to_json method to convert the data and then manipulated it with javascript. This gave me the flexibility to loop through the data pretty seamlessly. Atleast it seemed to work for my particular situation. Thanks for the help guys!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.