rails/actionpack-action_caching

layout: false not working

Closed this issue · 7 comments

Hey guys,

I'm having an issue where

caches_action :index, layout: false

doesn't actually render the layout correctly. It just renders the html resulting from the action, and doesn't add layout file around it.

I'm using rails 5, and I'm using 442930e on master of this repo.

I spent some time debugging, and it appears to be this commit that is causing the problem 9dd3a71

I followed the issue down to https://github.com/rails/rails/blob/v5.0.0.1/actionview/lib/action_view/renderer/template_renderer.rb#L96 where it is trying to resolve the layout, however the formats are only [:text], and it needs format [:html] to find the default layout/application.html.slim layout. When I change this line https://github.com/rails/actionpack-action_caching/blob/master/lib/action_controller/caching/actions.rb#L188 to use controller.render_to_string(text: body, layout: true)

it works as expected.

As you can probably tell from my description, I'm a little out of my depth here, so I can't really suggest a solution. Can anyone confirm that this is actually a bug?

Thanks for your time.

For anyone just looking for a quick fix, here is the monkey patch that works for me.

module ActionController
  module Caching
    module Actions
      class ActionCacheFilter
        def render_to_string(controller, body)
          controller.render_to_string(text: body, layout: true)
        end
      end
    end
  end
end

@dedman does the problem go away if you use ERB templates instead of Slim ?

Hey @pixeltrix no, I can still see the issue when using an ERB templates for both the layout, and view.

@dedman I assume that you get a deprecation warning about using :text in render_to_string?

Hey @pixeltrix yes, I'm getting the deprecation warning when using the monkeypatch above. I've tried raw, html options too and neither work as expected.

@dedman right, after banging my head against the wall for a couple of hours of failing to reproduce the bug in the test suite I realised that the layout was called talk_from_action.erb - i.e. no format in the name. 🤕

So that's a better workaround for the moment - just remove the format from the layout and it should find it. A simple fix would just be to change render_to_string from plain to html but that wouldn't fix the issue for other formats, e.g. XML (not sure how many apps do action caching with non-HTML templates).

I had the same problem. Downgrading to 1.1.1 fixed the problem for me.