Ruby on Rails Thursday, November 30, 2017

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
https://github.com/rails/rails/blob/cf5bc0dd5359e61dcb93e719279c7198e68a0eb8/actionview/lib/action_view/helpers/cache_helper.rb

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



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f8c54f86-080c-4c5c-b21d-f2eb3f622d5d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment