2010-02-13 66 views
0

我有一组类别,我通过JSON在我的应用程序的平均使用过程中多次通过javascript访问这些分类。如何根据respond_to类型返回memcached字符串?

所以在我的控制目前我有一个

@categories = Category.all 

respond_to do |format| 
    format.html # index.html.erb 
    format.json 

与格式什么,我需要尽可能JSON适当index.json.erb文件一起。

现在我想在我加入

的index.json.erb文件添加一些memcached的功能,这使
<% cache "JSON_CATEGORIES_ALL" do -%> block around my output 

我的问题是如何得到我的控制器,以回应时称此缓存键一个JSON请求并正常动作,从其他调用中从数据库中提取?

感谢

回答

1

您可以检查请求的格式:

@categories = Category.all unless request.format == "application/json" and fragment_exists?("JSON_CATEGORIES_ALL") 

respond_to do |format| 
    format.html # @categories is available 
    format.json # no database call if your cache fragment already exists 
end 
+0

雅那是接近我一直在用......我想通了,为什么它不工作见下文。 – 2010-02-14 00:55:07

0

我想通了......的人谁在这个跌倒在这儿呢。

@categories = Category.all unless request.format == "application/json" and Rails.cache.exist?("views/JSON_CATEGORIES_ALL") 

注意:加入意见/到缓存关键!似乎rails会将此视为缓存。

agregoire:谢谢你的

request.format == "application/json" 
相关问题