我有一个页面,我正在迭代一组具有多个所有者的行程并在页面上显示所有者图像。我想在视图中缓存整个所有者集,而不仅仅是单个部分。
<%
Rails.cache.fetch("trip_owners_#{trip.id.to_s}", expires_in: 7.days) do
trip.owners.limit(3).each do |owner|
%>
<%=render_user_card(owner)%>
<%
end
end
%>
render_user_card
基本上会呈现个人所有者的一部分。
def render_user_card(user, options = {})
return "" unless user
render :partial => 'users/user_card'
end
问题是我一直收到这个错误:
You are trying to cache a Ruby object which cannot be serialized to memcached.
根据我的理解,我必须缓存一个字符串。但是,我不知道该怎么做。如何在旅行中为所有用户存储组合的部分并仍然能够渲染部分?如果我将它存储为字符串,它将呈现为字符串而不是带有图像的部分字符串。
答案 0 :(得分:0)
我能够弄清楚如何做到这一点。见下文。
<%=
Rails.cache.fetch("trip_owners_#{trip.id.to_s}", expires_in: 7.days) do
output = ""
trip.owners.limit(3).each do |owner|
output += render_user_card(owner)
end
output
end.html_safe
%>