您在这里有两个选择:
选项 1:不缓存选票
这是最简单的解决方案,也是我个人推荐的解决方案。你只是不缓存动态用户相关部分,所以你有这样的东西:
# posts/_post.html.slim
- cache post do
  h1 = post.title
  = post.text
= render 'votes/form', post: post # not cached
选项 2:使用 javascript
这个解决方案更复杂,但实际上是 basecamp 的做法(但主要是更简单的示例)。您在页面上呈现了这两个部分,但使用 javascript 删除了其中一个。这是一个使用 jQuery 和 CoffeeScript 的示例:
# posts/_post.html.slim
- cache post do
  h1 = post.title
  = post.text
  = render 'votes/form', post: post
# votes/_form.html.slim
div#votes{"data-id" => post.id}
  .not_voted
    = form_for current_user.votes.find_by(post: post), method: :delete do |f|
      = f.submit
  .voted
    = form_for Vote.new do |f|
      = f.submit
# css
.not_voted {
  display:none;
}
# javascript (coffeescript)
jQuery ->
  if $('#votes').length
    $.getScript('/posts/current/' + $('#votes').data('id'))
# posts_controller.b
def current
  @post = Post.find(params[:id])
end
# users/current.js.erb
<% signed_in? && current_user.voted?(@post) %>
  $('.voted').hide();
  $('.not_voted').show();
<% end %>
但是,我会正确更改voted?方法以接受 id,因此您无需进行新查询。您可以在此 railscasts 中了解有关此方法的更多信息:http: //railscasts.com/episodes/169-dynamic-page-caching-revised?view=asciicast