Skip to content

Commit 5d31989

Browse files
committed
Use render_to_string :html when not caching layout
If a layout is using a format in it's name (e.g. layout.html.erb) then using render_to_string :plain breaks because it's looking for the text version of the layout (e.g. layout.text.erb). We can fix this by using render_to_string :layout, which restores the old behavior with render_to_string :text but there's still a bug with non-HTML layouts. Fixes #38.
1 parent 442930e commit 5d31989

File tree

4 files changed

+21
-25
lines changed

4 files changed

+21
-25
lines changed

lib/action_controller/caching/actions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def render_to_string(controller, body)
185185
end
186186
else
187187
def render_to_string(controller, body)
188-
controller.render_to_string(plain: body, layout: true)
188+
controller.render_to_string(html: body, layout: true)
189189
end
190190
end
191191
end

test/caching_test.rb

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ class ActionCachingTestController < CachingController
2727

2828
self.view_paths = FIXTURE_LOAD_PATH
2929

30-
# Eliminate uninitialized ivar warning
31-
before_action { @title = nil }
32-
3330
before_action only: :with_symbol_format do
3431
request.params[:format] = :json
3532
end
@@ -68,8 +65,7 @@ def forbidden
6865

6966
def with_layout
7067
@cache_this = MockTime.now.to_f.to_s
71-
@title = nil
72-
render plain: @cache_this, layout: true
68+
render html: @cache_this, layout: true
7369
end
7470

7571
def with_format_and_http_param
@@ -198,7 +194,7 @@ def params
198194
end
199195

200196
def request
201-
Object.new.instance_eval(<<-EVAL)
197+
Object.new.instance_eval <<-EVAL
202198
def path; "#{@mock_path}" end
203199
def format; "all" end
204200
def parameters; { format: nil }; end
@@ -296,15 +292,15 @@ def test_action_cache_with_layout_and_layout_cache_false
296292
get "/action_caching_test/layout_false", to: "action_caching_test#layout_false"
297293
end
298294

299-
get :layout_false
295+
get :layout_false, params: { title: "Request 1" }
300296
assert_response :success
301297
cached_time = content_to_cache
302-
assert_not_equal cached_time, @response.body
303-
assert fragment_exist?("hostname.com/action_caching_test/layout_false")
298+
assert_equal "<title>Request 1</title>\n#{cached_time}", @response.body
299+
assert_equal cached_time, read_fragment("hostname.com/action_caching_test/layout_false")
304300

305-
get :layout_false
301+
get :layout_false, params: { title: "Request 2" }
306302
assert_response :success
307-
assert_not_equal cached_time, @response.body
303+
assert_equal "<title>Request 2</title>\n#{cached_time}", @response.body
308304
assert_equal cached_time, read_fragment("hostname.com/action_caching_test/layout_false")
309305
end
310306

@@ -313,15 +309,15 @@ def test_action_cache_with_layout_and_layout_cache_false_via_proc
313309
get "/action_caching_test/with_layout_proc_param", to: "action_caching_test#with_layout_proc_param"
314310
end
315311

316-
get :with_layout_proc_param, params: { layout: "false" }
312+
get :with_layout_proc_param, params: { title: "Request 1", layout: "false" }
317313
assert_response :success
318314
cached_time = content_to_cache
319-
assert_not_equal cached_time, @response.body
320-
assert fragment_exist?("hostname.com/action_caching_test/with_layout_proc_param")
315+
assert_equal "<title>Request 1</title>\n#{cached_time}", @response.body
316+
assert_equal cached_time, read_fragment("hostname.com/action_caching_test/with_layout_proc_param")
321317

322-
get :with_layout_proc_param, params: { layout: "false" }
318+
get :with_layout_proc_param, params: { title: "Request 2", layout: "false" }
323319
assert_response :success
324-
assert_not_equal cached_time, @response.body
320+
assert_equal "<title>Request 2</title>\n#{cached_time}", @response.body
325321
assert_equal cached_time, read_fragment("hostname.com/action_caching_test/with_layout_proc_param")
326322
end
327323

@@ -330,16 +326,16 @@ def test_action_cache_with_layout_and_layout_cache_true_via_proc
330326
get "/action_caching_test/with_layout_proc_param", to: "action_caching_test#with_layout_proc_param"
331327
end
332328

333-
get :with_layout_proc_param, params: { layout: "true" }
329+
get :with_layout_proc_param, params: { title: "Request 1", layout: "true" }
334330
assert_response :success
335331
cached_time = content_to_cache
336-
assert_not_equal cached_time, @response.body
337-
assert fragment_exist?("hostname.com/action_caching_test/with_layout_proc_param")
332+
assert_equal "<title>Request 1</title>\n#{cached_time}", @response.body
333+
assert_equal "<title>Request 1</title>\n#{cached_time}", read_fragment("hostname.com/action_caching_test/with_layout_proc_param")
338334

339-
get :with_layout_proc_param, params: { layout: "true" }
335+
get :with_layout_proc_param, params: { title: "Request 2", layout: "true" }
340336
assert_response :success
341-
assert_not_equal cached_time, @response.body
342-
assert_equal @response.body, read_fragment("hostname.com/action_caching_test/with_layout_proc_param")
337+
assert_equal "<title>Request 1</title>\n#{cached_time}", @response.body
338+
assert_equal "<title>Request 1</title>\n#{cached_time}", read_fragment("hostname.com/action_caching_test/with_layout_proc_param")
343339
end
344340

345341
def test_action_cache_conditional_options

test/fixtures/layouts/talk_from_action.erb

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<title><%= params[:title] %></title>
2+
<%= yield -%>

0 commit comments

Comments
 (0)