ApplicationController中的缓存方法

时间:2012-05-06 16:49:40

标签: ruby-on-rails ruby ruby-on-rails-3

我在ApplicationController中创建了一个帮助方法,允许访问所有页面上的最新帖子。

有什么方法可以缓存这个以阻止我的应用程序经常访问数据库?

class ApplicationController < ActionController::Base
  protect_from_forgery
  helper_method :recent_posts

  def recent_posts
    @recent_posts = Post.published.recent
  end
end

我已经尝试过cache_action:recent_posts但是,通过查看日志,应用程序仍然会出现在数据库中。

1 个答案:

答案 0 :(得分:0)

听起来你正在寻找fragment caching

尝试将其置于局部并执行以下操作:

<% cache do %>
  <% Posts.published.recent.each do |p| %>
    <%= link_to p.name, post_url(p) %>
  <% end %>
<% end %>
相关问题