diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2008-12-25 22:11:06 +0000 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-12-25 22:26:03 +0000 |
commit | d67e03871eabb912434dafac3eeb8e6ea7c5585f (patch) | |
tree | ac7d7092f56c5e5bfe6c690d44bbf5c91fca8c24 | |
parent | 061952392afd1dae1aa97a816e9a0c79df7c4514 (diff) | |
download | rails-d67e03871eabb912434dafac3eeb8e6ea7c5585f.tar.gz rails-d67e03871eabb912434dafac3eeb8e6ea7c5585f.tar.bz2 rails-d67e03871eabb912434dafac3eeb8e6ea7c5585f.zip |
Make ActionController#render(string) work as a shortcut for render :template => string. [#1435]
Examples:
# Instead of render(:template => 'controller/action')
render('controller/action')
Note : Argument must not begin with a '/', but have at least one '/'
-rw-r--r-- | actionpack/CHANGELOG | 7 | ||||
-rw-r--r-- | actionpack/lib/action_controller/base.rb | 4 | ||||
-rw-r--r-- | actionpack/test/controller/render_test.rb | 21 |
3 files changed, 28 insertions, 4 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 5906ab1f16..6badb412f8 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,11 +1,12 @@ *2.3.0 [Edge]* -* Make ActionController#render(string) work as a shortcut for render :file => string. [#1435] [Pratik Naik] Examples: +* Make ActionController#render(string) work as a shortcut for render :file/:template => string. [#1435] [Pratik Naik] Examples: # Instead of render(:file => '/Users/lifo/home.html.erb') - render('/Users/lifo/home.html.erb') + render('/Users/lifo/home.html.erb') # argument must begin with a '/' - Note : Filename must begin with a forward slash ('/') + # Instead of render(:template => 'controller/action') + render('controller/action') # argument must not begin with a '/', but contain a '/' * Add :prompt option to date/time select helpers. #561 [Sam Oliver] diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 9bf044b6c0..29f1c84f03 100644 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -866,9 +866,11 @@ module ActionController #:nodoc: elsif options == :update options = extra_options.merge({ :update => true }) elsif options.is_a?(String) - case options.index('/') + case position = options.index('/') when 0 extra_options[:file] = options + else + extra_options[:template] = options end options = extra_options diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index f2393e695a..ce9756a060 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -202,6 +202,11 @@ class TestController < ActionController::Base render :template => "test/hello" end + def render_xml_hello_as_string_template + @name = "David" + render "test/hello" + end + def render_xml_with_custom_content_type render :xml => "<blah/>", :content_type => "application/atomsvc+xml" end @@ -332,6 +337,10 @@ class TestController < ActionController::Base render :template => "test/hello_world" end + def render_with_explicit_string_template + render "test/hello_world" + end + def render_with_explicit_template_with_locals render :template => "test/render_file_with_locals", :locals => { :secret => 'area51' } end @@ -654,6 +663,7 @@ class TestController < ActionController::Base "accessing_params_in_template", "accessing_params_in_template_with_layout", "render_with_explicit_template", + "render_with_explicit_string_template", "render_js_with_explicit_template", "render_js_with_explicit_action_template", "delete_with_js", "update_page", "update_page_with_instance_variables" @@ -888,6 +898,12 @@ class RenderTest < ActionController::TestCase assert_equal "application/xml", @response.content_type end + def test_render_xml_as_string_template + get :render_xml_hello_as_string_template + assert_equal "<html>\n <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body + assert_equal "application/xml", @response.content_type + end + def test_render_xml_with_default get :greeting assert_equal "<p>This is grand!</p>\n", @response.body @@ -1073,6 +1089,11 @@ class RenderTest < ActionController::TestCase assert_response :success end + def test_render_with_explicit_string_template + get :render_with_explicit_string_template + assert_equal "<html>Hello world!</html>", @response.body + end + def test_double_render assert_raises(ActionController::DoubleRenderError) { get :double_render } end |