作者ayamomiji (ayaya)
看板Ruby
标题[问题] Rails Cache
时间Sun Nov 6 01:08:31 2011
环境:
e.g. Ruby 1.9.3, Rails 3.1.1
状况:
目前我有一个页面大致上是长这个样子的 (in slim):
#posts
- cache [@posts.latest, @posts.current_page, current_user] do
- @posts.each do |post|
= post.title
= post.body
.links
= edit_post_link(post) if current_user.can?(:edit, post)
= paginate @posts
我想将整个 #posts cache, 但又希望 .links 因人而异,
所以我的 cache key 就必须含有最新一篇 post, 目前页数以及 current_user,
而我希望能够指定某一部份不要 cache (.links),
这样我就可以拿掉 current_user, 让所有 user 可以共用同一份 posts cache,
但我想这应该是做不到的事情? 所以我想从设计上着手,
目前我想到的两个方法是:
1. 把编辑的连结移到各个 posts 的 show action,
该页的 cache key 可以化简成 [@post, current_user.can?(:edit, post)]
2. 把 .links 移出 #posts 外, 页面读入时再用 js 组合起来
基於不想随意改变使用者习惯的前提下, 我不想选用 1 的方式, 但又觉得 2 好丑 XD
不知道有没有其他更好的方式去解决这个问题?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 58.114.185.227