Ruby on rails 视图中的Rails缓存--无法序列化为memcached的Ruby对象
我有一个页面,在这个页面上,我与多个所有者迭代了一组旅行,并在页面上显示了所有者图像。我希望在视图中缓存整个所有者集,而不仅仅是单个部分Ruby on rails 视图中的Rails缓存--无法序列化为memcached的Ruby对象,ruby-on-rails,ruby-on-rails-3,memcached,Ruby On Rails,Ruby On Rails 3,Memcached,我有一个页面,在这个页面上,我与多个所有者迭代了一组旅行,并在页面上显示了所有者图像。我希望在视图中缓存整个所有者集,而不仅仅是单个部分 <% 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
<%
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
%>
问题是,我一直遇到这样的错误:
You are trying to cache a Ruby object which cannot be serialized to memcached.
根据我的理解,我必须缓存一个字符串。然而,我不知道如何做到这一点。我如何为旅行中的所有用户存储合成的片段,并且仍然能够呈现片段?如果我将其存储为字符串,它将以字符串的形式呈现,而不是以带有图像的部分呈现。我能够想出如何做到这一点。见下文
<%=
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
%>
<%=
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
%>