Skip to content

Commit

Permalink
Make ActionController#render(string) work as a shortcut for render :f…
Browse files Browse the repository at this point in the history
…ile => string. [rails#1435]

Examples:
  # Instead of render(:file => '/Users/lifo/home.html.erb')
  render('/Users/lifo/home.html.erb')

Note : Filename must begin with a forward slash ('/')
  • Loading branch information
lifo committed Dec 25, 2008
1 parent dd07534 commit 0619523
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
7 changes: 7 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,12 @@
*2.3.0 [Edge]*

* Make ActionController#render(string) work as a shortcut for render :file => string. [#1435] [Pratik Naik] Examples:

# Instead of render(:file => '/Users/lifo/home.html.erb')
render('/Users/lifo/home.html.erb')

Note : Filename must begin with a forward slash ('/')

* Add :prompt option to date/time select helpers. #561 [Sam Oliver]

* Fixed that send_file shouldn't set an etag #1578 [Hongli Lai]
Expand Down
9 changes: 8 additions & 1 deletion actionpack/lib/action_controller/base.rb
Expand Up @@ -865,6 +865,13 @@ def render(options = nil, extra_options = {}, &block) #:doc:
return render(:file => default_template, :layout => true)
elsif options == :update
options = extra_options.merge({ :update => true })
elsif options.is_a?(String)
case options.index('/')
when 0
extra_options[:file] = options
end

options = extra_options
end

layout = pick_layout(options)
Expand Down Expand Up @@ -1183,7 +1190,7 @@ def render_for_text(text = nil, status = nil, append_response = false) #:nodoc:
end

def validate_render_arguments(options, extra_options)
if options && options != :update && !options.is_a?(Hash)
if options && options != :update && !options.is_a?(String) && !options.is_a?(Hash)
raise RenderError, "You called render with invalid options : #{options.inspect}"
end

Expand Down
29 changes: 21 additions & 8 deletions actionpack/test/controller/render_test.rb
Expand Up @@ -104,6 +104,12 @@ def render_file_with_instance_variables
render :file => path
end

def render_file_as_string_with_instance_variables
@secret = 'in the sauce'
path = File.expand_path(File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_ivar.erb'))
render path
end

def render_file_not_using_full_path
@secret = 'in the sauce'
render :file => 'test/render_file_with_ivar'
Expand All @@ -124,6 +130,11 @@ def render_file_with_locals
render :file => path, :locals => {:secret => 'in the sauce'}
end

def render_file_as_string_with_locals
path = File.expand_path(File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_locals.erb'))
render path, :locals => {:secret => 'in the sauce'}
end

def accessing_request_in_template
render :inline => "Hello: <%= request.host %>"
end
Expand Down Expand Up @@ -182,10 +193,6 @@ def render_nothing_with_appendix
render :text => "appended"
end

def render_invalid_args
render("test/hello")
end

def render_vanilla_js_hello
render :js => "alert('hello')"
end
Expand Down Expand Up @@ -751,6 +758,11 @@ def test_render_file_with_instance_variables
assert_equal "The secret is in the sauce\n", @response.body
end

def test_render_file_as_string_with_instance_variables
get :render_file_as_string_with_instance_variables
assert_equal "The secret is in the sauce\n", @response.body
end

def test_render_file_not_using_full_path
get :render_file_not_using_full_path
assert_equal "The secret is in the sauce\n", @response.body
Expand All @@ -766,6 +778,11 @@ def test_render_file_with_locals
assert_equal "The secret is in the sauce\n", @response.body
end

def test_render_file_as_string_with_locals
get :render_file_as_string_with_locals
assert_equal "The secret is in the sauce\n", @response.body
end

def test_render_file_from_template
get :render_file_from_template
assert_equal "The secret is in the sauce\n", @response.body
Expand Down Expand Up @@ -831,10 +848,6 @@ def test_render_nothing_with_appendix
assert_equal 'appended', @response.body
end

def test_attempt_to_render_with_invalid_arguments
assert_raises(ActionController::RenderError) { get :render_invalid_args }
end

def test_attempt_to_access_object_method
assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
end
Expand Down

0 comments on commit 0619523

Please sign in to comment.