Skip to content

Commit

Permalink
Added that requests with JavaScript as the priority mime type in the …
Browse files Browse the repository at this point in the history
…accept header and no format extension in the parameters will be treated as though their format was :js when it comes to determining which template to render. This makes it possible for JS requests to automatically render action.js.rjs files without an explicit respond_to block [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8956 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Feb 29, 2008
1 parent 18aa19c commit 7dcd0d7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* Added that requests with JavaScript as the priority mime type in the accept header and no format extension in the parameters will be treated as though their format was :js when it comes to determining which template to render. This makes it possible for JS requests to automatically render action.js.rjs files without an explicit respond_to block [DHH]

* Tests for distance_of_time_in_words with TimeWithZone instances. Closes #10914 [ernesto.jimenez]

* Remove support for multivalued (e.g., '&'-delimited) cookies. [Jamis Buck]
Expand Down
23 changes: 21 additions & 2 deletions actionpack/lib/action_view/base.rb
Expand Up @@ -344,10 +344,29 @@ def file_public?(template_path)#:nodoc:
end

# symbolized version of the :format parameter of the request, or :html by default.
#
# EXCEPTION: If the :format parameter is not set, the Accept header will be examined for
# whether it contains the JavaScript mime type as its first priority. If that's the case,
# it will be used. This ensures that Ajax applications can use the same URL to support both
# JavaScript and non-JavaScript users.
def template_format
return @template_format if @template_format
format = controller && controller.respond_to?(:request) && controller.request.parameters[:format]
@template_format = format.blank? ? :html : format.to_sym

if controller && controller.respond_to?(:request)
parameter_format = controller.request.parameters[:format]
accept_format = controller.request.accepts.first

case
when parameter_format.blank? && accept_format != :js
@template_format = :html
when parameter_format.blank? && accept_format == :js
@template_format = :js
else
@template_format = parameter_format.to_sym
end
else
@template_format = :html
end
end

private
Expand Down
6 changes: 6 additions & 0 deletions actionpack/test/controller/new_render_test.rb
Expand Up @@ -550,6 +550,12 @@ def test_render_xml_with_default
assert_equal "<p>This is grand!</p>\n", @response.body
end

def test_render_with_default_from_accept_header
@request.env["HTTP_ACCEPT"] = "text/javascript"
get :greeting
assert_equal "$(\"body\").visualEffect(\"highlight\");", @response.body
end

def test_render_rjs_with_default
get :delete_with_js
assert_equal %!Element.remove("person");\nnew Effect.Highlight(\"project-4\",{});!, @response.body
Expand Down
1 change: 1 addition & 0 deletions actionpack/test/fixtures/test/greeting.js.rjs
@@ -0,0 +1 @@
page[:body].visual_effect :highlight

0 comments on commit 7dcd0d7

Please sign in to comment.