2013-03-29 52 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 
%>