Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在Rails 4中缓存个性化片段?_Ruby On Rails_Ruby_Caching - Fatal编程技术网

Ruby on rails 如何在Rails 4中缓存个性化片段?

Ruby on rails 如何在Rails 4中缓存个性化片段?,ruby-on-rails,ruby,caching,Ruby On Rails,Ruby,Caching,我的应用程序(Rails 4)允许用户对帖子进行投票。是否可以缓存帖子,但对投票缓存进行个性化设置,以便显示当前用户的个性化投票?例如,用户是否投票 我宁愿不改变html结构来实现这一点 # posts/_post.html.slim - cache post do h1 = post.title = post.text = render 'votes/form', post: post # votes/_form.html.slim - if signed_in? &&a

我的应用程序(Rails 4)允许用户对帖子进行投票。是否可以缓存帖子,但对投票缓存进行个性化设置,以便显示当前用户的个性化投票?例如,用户是否投票

我宁愿不改变html结构来实现这一点

# posts/_post.html.slim
- cache post do
  h1 = post.title
  = post.text
  = render 'votes/form', post: post

# votes/_form.html.slim
- if signed_in? && current_user.voted?(post)
  = form_for current_user.votes.find_by(post: post), method: :delete do |f|
    = f.submit
- else
  = form_for Vote.new do |f|
    = f.submit

这里有两个选项:

选项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 %>
#posts/_post.html.slim
-缓存post do
h1=职位名称
=post.text
=呈现“投票/表格”,张贴:张贴
#投票/_form.html.slim
div#投票{“数据id”=>post.id}
.没有投票
=当前用户的表单。投票。通过(post:post)查找表单,方法::delete do | f|
=f.提交
.投票
=表格U供投票。新do | f|
=f.提交
#css
.没有投票{
显示:无;
}
#javascript(咖啡脚本)
jQuery->
如果$('#投票')。长度
$.getScript('/posts/current/'+$('#投票')。数据('id'))
#邮政署署长
def电流
@post=post.find(参数[:id])
结束
#用户/current.js.erb
$('.voted').hide();
$('.not_voted').show();

不过,我会适当地更改
voated?
方法以接受id,这样您就不需要进行新的查询。您可以在railscasts中了解更多有关此方法的信息:

尝试以下方法,这将为每个帖子的已投票和未投票创建两个不同的片段。它将根据其状态被读取

# posts/_post.html.slim
- cache [post, current_user.votes.find_by(post: post)]do
  h1 = post.title
  = post.text
  = render 'votes/form', post: post

谢谢更改投票以接收id如何阻止它执行额外的查询?它不是已经可以作为实例变量使用了吗?您需要的唯一查询是投票。其中user_id=foo和post_id=foo。是的,但使用javascript解决方案将
$。getScript
触发ajax请求,然后通过db查询设置
@post
实例变量。如果你能有一个
@post\u id
,这会更快吗?这在这个场景中很重要。谢谢。最后一点意见。您的第一个解决方案意味着我无法呈现集合,如
=render@posts
中所述,因为呈现投票将在
\u post
部分中发生,并且将缓存它。我需要在集合上循环,分别缓存和渲染每个帖子,然后在缓存块之外渲染
\u投票
部分。对吗?对,但是当用户对帖子进行投票时,您可以通过触摸帖子(调用
touch
或在帖子上添加
:touch=>true
)来解决这个问题。