Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added render :js for people who want to render inline JavaScript repl…

…ies without using RJS [DHH]
  • Loading branch information...
commit cbeac93310a7e95453bea3f2d4551288fd455d07 1 parent 408c722
@dhh dhh authored
View
24 actionpack/lib/action_controller/base.rb
@@ -801,6 +801,19 @@ def append_view_path(path)
# # Renders "Hello from code!"
# render :text => proc { |response, output| output.write("Hello from code!") }
#
+ # === Rendering XML
+ #
+ # Rendering XML sets the content type to application/xml.
+ #
+ # # Renders '<name>David</name>'
+ # render :xml => {:name => "David"}.to_xml
+ #
+ # It's not necessary to call <tt>to_xml</tt> on the object you want to render, since <tt>render</tt> will
+ # automatically do that for you:
+ #
+ # # Also renders '<name>David</name>'
+ # render :xml => {:name => "David"}
+ #
# === Rendering JSON
#
# Rendering JSON sets the content type to application/json and optionally wraps the JSON in a callback. It is expected
@@ -846,7 +859,12 @@ def append_view_path(path)
# page.visual_effect :highlight, 'user_list'
# end
#
- # === Rendering with status and location headers
+ # === Rendering vanilla JavaScript
+ #
+ # In addition to using RJS with render :update, you can also just render vanilla JavaScript with :js.
+ #
+ # # Renders "alert('hello')" and sets the mime type to text/javascript
+ # render :js => "alert('hello')"
#
# All renders take the <tt>:status</tt> and <tt>:location</tt> options and turn them into headers. They can even be used together:
#
@@ -898,6 +916,10 @@ def render(options = nil, extra_options = {}, &block) #:doc:
response.content_type ||= Mime::XML
render_for_text(xml.respond_to?(:to_xml) ? xml.to_xml : xml, options[:status])
+ elsif js = options[:js]
+ response.content_type ||= Mime::JS
+ render_for_text(js, options[:status])
+
elsif json = options[:json]
json = json.to_json unless json.is_a?(String)
json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
View
10 actionpack/test/controller/render_test.rb
@@ -184,6 +184,10 @@ def render_invalid_args
render("test/hello")
end
+ def render_vanilla_js_hello
+ render :js => "alert('hello')"
+ end
+
def render_xml_hello
@name = "David"
render :template => "test/hello"
@@ -844,6 +848,12 @@ def test_access_to_controller_name_in_view
assert_equal "test", @response.body # name is explicitly set to 'test' inside the controller.
end
+ def test_render_vanilla_js
+ get :render_vanilla_js_hello
+ assert_equal "alert('hello')", @response.body
+ assert_equal "text/javascript", @response.content_type
+ end
+
def test_render_xml
get :render_xml_hello
assert_equal "<html>\n <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body
View
2  activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*2.2.1 [RC2 or 2.2 final]*
+* Added render :js for people who want to render inline JavaScript replies without using RJS [DHH]
+
* Fixed the option merging in Array#to_xml #1126 [Rudolf Gavlas]
* Make I18n::Backend::Simple reload its translations in development mode [DHH/Sven Fuchs]
Please sign in to comment.
Something went wrong with that request. Please try again.