开发者

How to update variable dynamically within Rails app

I am writing my first Rails app using the twitte开发者_如何转开发r 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.


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.


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?


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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜