Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make sure respond_with with :js tries to render a template in all cases

  • Loading branch information...
commit 9c9ec2172e46710d1d5ac513a4ff52df35fef51f 1 parent 35cdd25
@josevalim josevalim authored
View
2  actionpack/CHANGELOG
@@ -30,6 +30,8 @@
*Rails 3.1.0 (unreleased)*
+* Make sure respond_with with :js tries to render a template in all cases [José Valim]
+
* json_escape will now return a SafeBuffer string if it receives SafeBuffer string [tenderlove]
* Make sure escape_js returns SafeBuffer string if it receives SafeBuffer string [Prem Sichanugrist]
View
5 actionpack/lib/action_controller/metal/responder.rb
@@ -162,6 +162,11 @@ def to_html
navigation_behavior(e)
end
+ # to_js simply tries to render a template. If no template is found, raises the error.
+ def to_js
+ default_render
+ end
+
# All other formats follow the procedure below. First we try to render a
# template, if the template is not available, we verify if the resource
# responds to :to_format and display it.
View
21 actionpack/test/controller/mime_responds_test.rb
@@ -509,7 +509,7 @@ def test_invalid_format
class RespondWithController < ActionController::Base
respond_to :html, :json
respond_to :xml, :except => :using_resource_with_block
- respond_to :js, :only => [ :using_resource_with_block, :using_resource ]
+ respond_to :js, :only => [ :using_resource_with_block, :using_resource, :using_hash_resource ]
def using_resource
respond_with(resource)
@@ -575,11 +575,6 @@ def respond; @controller.render :text => "respond #{format}"; end
def resource
Customer.new("david", request.delete? ? nil : 13)
end
-
- def _render_js(js, options)
- self.content_type ||= Mime::JS
- self.response_body = js.respond_to?(:to_js) ? js.to_js : js
- end
end
class InheritedRespondWithController < RespondWithController
@@ -638,6 +633,20 @@ def test_using_resource
end
end
+ def test_using_resource_with_js_simply_tries_to_render_the_template
+ @request.accept = "text/javascript"
+ get :using_resource
+ assert_equal "text/javascript", @response.content_type
+ assert_equal "alert(\"Hi\");", @response.body
+ end
+
+ def test_using_hash_resource_with_js_raises_an_error_if_template_cant_be_found
+ @request.accept = "text/javascript"
+ assert_raise ActionView::MissingTemplate do
+ get :using_hash_resource
+ end
+ end
+
def test_using_hash_resource
@request.accept = "application/xml"
get :using_hash_resource
View
1  actionpack/test/fixtures/respond_with/using_resource.js.erb
@@ -0,0 +1 @@
+alert("Hi");

2 comments on commit 9c9ec21

@tute

Hi! I'm using ActionController::Responder with a jQuery Mobile UI. Accept of a POST request is /;q=0.5, text/javascript, application/javascript[...], request format is seen as js (would like it to be JSON, I'm now researching on how to do it). Anyhow, the question is: why with JS requests would we want to always have a template? One problem of this approach is that even though my entry may not be saved due to validation errors, response HTTP code is 200 OK.
Thanks!

@tute

Now sending JSON requests, but anyhow I'd like to know the reason why JS requests would always need a template.
Thanks,
Tute.

Please sign in to comment.
Something went wrong with that request. Please try again.