2013-03-29 51 views
0

我有一个页面,我正在遍历一组具有多个所有者的行程并在页面上显示所有者图像。我想在视图中缓存整个所有者集合,而不仅仅是单个部分。Rails缓存视图 - 无法序列化为memcached的Ruby对象

<% 
    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

我能弄清楚如何做到这一点。见下文。

<%= 
    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 
%>