Caching, Collections, Rails 5, multiple views colliding

瀏覽次數:10 次
跳到第一則未讀訊息

Rob Jonson

未讀,
2017年11月30日 上午8:07:132017/11/30
收件者:Ruby on Rails: Talk
I have a Thing object that can be presented in two ways depending on whether you are in the main controller or the list controller.

main/_thing.rb
list/_thing.rb

I can cache both of these without them clobbering each others' keys

main/_thing.rb
<% cache ["main",thing] do %>
-stuff-
<% end %>

and

list/_thing.rb
<% cache ["list",thing] do %>
-stuff-
<% end %>


Rails 5 has the shiny new cache capability for collections

<%= render partial: "main/thing", collection: myThings, cached: true  %>

this works great

however I'd also like to the same approach in my list view with

<%= render partial: "list/thing", collection: myThings, cached: true  %>

but now I have conflicting keys and everything goes squirly. I think because the cache key is generated on Thing and doesn't 'realise' that I'm using different presentation logic.
there is one hint of an error I get

'Couldn't find template for digesting: list/_thing'
I don't understand this error; list/_thing.html.erb definitely does exist.


I can't for the life of me figure out how to get a custom cache key (and for a while I accidentally broke production before realising the issue here).

The relevant rails code is here I think

and it looks like this should be possible.

I have tried

cached: -> myThings { [ myThings, "list" ] }
and
cached: [myThings, "list" ] 

but those were really just guesses as I don't understand the logic

I'm running rails 5.0.6

can anyone help?

thank you.

Rob



回覆所有人
回覆作者
轉寄
0 則新訊息