开发者

How can I access a Controller's class variable in a view

I'm basically trying to cache tweets in my开发者_JAVA百科 Rails 3 app so I don't have to get them on every page refresh. I have two class variables:

@@tweet_time = Time.now
@@tweets = Twitter.user_timeline(TWEET_ACCOUNT)[0,NUMBER_OF_TWEETS]

Those are in my controller.

In the controller, I check to see if

@@tweet_time < 1.hour.ago

and if so I reset @@tweets and @@tweet_time.

But when I try to loop through @@tweets in the view, it says @@tweets is an uninitialized class variable. I guess that is because technically it is being called from ActionView instead of ActionController.

How can I access @@tweets in the view?

PS If I am doing this completely backwards, feel free to critique and hopefully offer me a better way. I really do appreciate any feedback you can offer.


I don't think that class variables in the controller will persist across requests. That's not the duty of the controller.

If you don't want to use an in-memory database like Redis to cache your tweets, you can try moving your class variable to a model which you can then query from the controller.


I am using the "dalli" gem to do this now. It is a speedy way to use MemCache from what I can tell.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜