Skip to content

Commit bc5fd3e

Browse files
authored
Merge pull request #39497 from jhawthorn/render_with_invalid_options
Raise when calling render with invalid options
2 parents 24d47be + e53e2a0 commit bc5fd3e

File tree

9 files changed

+43
-22
lines changed

9 files changed

+43
-22
lines changed

actionpack/lib/action_controller/metal/rendering.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ def _normalize_text(options)
115115

116116
# Process controller specific options, as status, content-type and location.
117117
def _process_options(options)
118-
status, content_type, location = options.values_at(:status, :content_type, :location)
118+
status = options.delete(:status)
119+
content_type = options.delete(:content_type)
120+
location = options.delete(:location)
119121

120122
self.status = status if status
121123
self.content_type = content_type if content_type

actionpack/test/controller/render_test.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -371,15 +371,6 @@ def test_dynamic_render
371371
end
372372
end
373373

374-
def test_permitted_dynamic_render_file_hash
375-
assert File.exist?(File.expand_path("../../test/abstract_unit.rb", __dir__))
376-
assert_deprecated do
377-
assert_raises ActionView::MissingTemplate do
378-
get :dynamic_render_permit, params: { id: { file: '../\\../test/abstract_unit.rb' } }
379-
end
380-
end
381-
end
382-
383374
def test_dynamic_render_file_hash
384375
assert_raises ArgumentError do
385376
get :dynamic_render, params: { id: { file: '../\\../test/abstract_unit.rb' } }

actionpack/test/controller/renderers_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ def test_using_custom_render_option
6969
ActionController.remove_renderer :simon
7070
end
7171

72-
def test_raises_missing_template_no_renderer
73-
assert_raise ActionView::MissingTemplate do
72+
def test_raises_argument_error_no_renderer
73+
assert_raise ArgumentError do
7474
get :respond_to_mime, format: "csv"
7575
end
7676
assert_equal Mime[:csv], @response.media_type

actionview/lib/action_view/renderer/abstract_renderer.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def format
158158

159159
def extract_details(options) # :doc:
160160
details = nil
161-
@lookup_context.registered_details.each do |key|
161+
details_arguments.each do |key|
162162
value = options[key]
163163

164164
if value
@@ -168,6 +168,10 @@ def extract_details(options) # :doc:
168168
details || NO_DETAILS
169169
end
170170

171+
def details_arguments
172+
@lookup_context.registered_details
173+
end
174+
171175
def prepend_formats(formats) # :doc:
172176
formats = Array(formats)
173177
return if formats.empty? || @lookup_context.html_fallback_for_js

actionview/lib/action_view/renderer/partial_renderer.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,13 @@ class PartialRenderer < AbstractRenderer
256256

257257
def initialize(lookup_context, options)
258258
super(lookup_context)
259+
260+
options.assert_valid_keys(
261+
:partial, :template, :renderable, :layout,
262+
:locals, :collection, :object, :as, :cached, :spacer_template,
263+
*details_arguments
264+
)
265+
259266
@options = options
260267
@locals = @options[:locals] || {}
261268
@details = extract_details(@options)

actionview/lib/action_view/renderer/template_renderer.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
module ActionView
44
class TemplateRenderer < AbstractRenderer #:nodoc:
55
def render(context, options)
6+
options.assert_valid_keys(
7+
:body, :plain, :html, :file, :inline, :template, :renderable,
8+
:layout, :locals, :prefixes,
9+
:type,
10+
*details_arguments
11+
)
12+
613
@details = extract_details(options)
714
template = determine_template(options)
815

actionview/lib/action_view/rendering.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def _normalize_options(options)
165165
options[:prefixes] ||= _prefixes
166166
end
167167

168-
options[:template] ||= (options[:action] || action_name).to_s
168+
options[:template] ||= (options.delete(:action) || action_name).to_s
169169
options
170170
end
171171
end

actionview/test/actionpack/abstract/abstract_controller_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def _prefixes
3939

4040
def render(options = {})
4141
if options.is_a?(String)
42-
options = { _template_name: options }
42+
options = { action: options }
4343
end
4444
super
4545
end
@@ -49,7 +49,7 @@ def render(options = {})
4949

5050
class Me2 < RenderingController
5151
def index
52-
render "index.erb"
52+
render "index"
5353
end
5454

5555
def index_to_string
@@ -58,7 +58,7 @@ def index_to_string
5858

5959
def action_with_ivars
6060
@my_ivar = "Hello"
61-
render "action_with_ivars.erb"
61+
render "action_with_ivars"
6262
end
6363

6464
def naked_render
@@ -200,7 +200,7 @@ def self.layout(formats)
200200
end
201201

202202
def render_to_body(options = {})
203-
options[:_layout] = options[:layout] || _default_layout({})
203+
options[:layout] = options[:layout] || _default_layout({})
204204
super
205205
end
206206
end

actionview/test/template/render_test.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ def test_render_without_options
6868
assert_match(/You invoked render but did not give any of (.+) option\./, e.message)
6969
end
7070

71+
def test_render_throws_exception_when_given_partial_and_invalid_options
72+
e = assert_raises(ArgumentError) { @view.render(template: "test/hello_world", invalid_option: true) }
73+
assert_includes e.message, "Unknown key: :invalid_option"
74+
end
75+
76+
def test_render_throws_exception_when_given_template_and_invalid_options
77+
e = assert_raises(ArgumentError) { @view.render(partial: "test/partial", invalid_option: true) }
78+
assert_includes e.message, "Unknown key: :invalid_option"
79+
end
80+
7181
def test_render_template
7282
assert_equal "Hello world!", @view.render(template: "test/hello_world")
7383
end
@@ -739,30 +749,30 @@ def setup
739749

740750
def test_render_utf8_template_with_magic_comment
741751
with_external_encoding Encoding::ASCII_8BIT do
742-
result = @view.render(template: "test/utf8_magic", formats: [:html], layouts: "layouts/yield")
752+
result = @view.render(template: "test/utf8_magic", formats: [:html])
743753
assert_equal Encoding::UTF_8, result.encoding
744754
assert_equal "\nРусский \nтекст\n\nUTF-8\nUTF-8\nUTF-8\n", result
745755
end
746756
end
747757

748758
def test_render_utf8_template_with_default_external_encoding
749759
with_external_encoding Encoding::UTF_8 do
750-
result = @view.render(template: "test/utf8", formats: [:html], layouts: "layouts/yield")
760+
result = @view.render(template: "test/utf8", formats: [:html])
751761
assert_equal Encoding::UTF_8, result.encoding
752762
assert_equal "Русский текст\n\nUTF-8\nUTF-8\nUTF-8\n", result
753763
end
754764
end
755765

756766
def test_render_utf8_template_with_incompatible_external_encoding
757767
with_external_encoding Encoding::SHIFT_JIS do
758-
e = assert_raises(ActionView::Template::Error) { @view.render(template: "test/utf8", formats: [:html], layouts: "layouts/yield") }
768+
e = assert_raises(ActionView::Template::Error) { @view.render(template: "test/utf8", formats: [:html], layout: "layouts/yield") }
759769
assert_match "Your template was not saved as valid Shift_JIS", e.cause.message
760770
end
761771
end
762772

763773
def test_render_utf8_template_with_partial_with_incompatible_encoding
764774
with_external_encoding Encoding::SHIFT_JIS do
765-
e = assert_raises(ActionView::Template::Error) { @view.render(template: "test/utf8_magic_with_bare_partial", formats: [:html], layouts: "layouts/yield") }
775+
e = assert_raises(ActionView::Template::Error) { @view.render(template: "test/utf8_magic_with_bare_partial", formats: [:html], layout: "layouts/yield") }
766776
assert_match "Your template was not saved as valid Shift_JIS", e.cause.message
767777
end
768778
end

0 commit comments

Comments
 (0)