public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Added render :js for people who want to render inline JavaScript replies without 
using RJS [DHH]
dhh (author)
Sat Nov 01 04:03:49 -0700 2008
commit  cbeac93310a7e95453bea3f2d4551288fd455d07
tree    fed49136581bf2f15b0616139b1e8ff0baed7434
parent  408c7227575e0c395a45605783cade1ba3b51e02
...
801
802
803
 
 
 
 
 
 
 
 
 
 
 
 
 
804
805
806
...
846
847
848
849
 
 
 
 
 
 
850
851
852
...
898
899
900
 
 
 
 
901
902
903
...
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
...
859
860
861
 
862
863
864
865
866
867
868
869
870
...
916
917
918
919
920
921
922
923
924
925
0
@@ -801,6 +801,19 @@ module ActionController #:nodoc:
0
       #   # Renders "Hello from code!"
0
       #   render :text => proc { |response, output| output.write("Hello from code!") }
0
       #
0
+      # === Rendering XML
0
+      #
0
+      # Rendering XML sets the content type to application/xml.
0
+      #
0
+      #   # Renders '<name>David</name>'
0
+      #   render :xml => {:name => "David"}.to_xml
0
+      #
0
+      # It's not necessary to call <tt>to_xml</tt> on the object you want to render, since <tt>render</tt> will
0
+      # automatically do that for you:
0
+      #
0
+      #   # Also renders '<name>David</name>'
0
+      #   render :xml => {:name => "David"}
0
+      #
0
       # === Rendering JSON
0
       #
0
       # Rendering JSON sets the content type to application/json and optionally wraps the JSON in a callback. It is expected
0
@@ -846,7 +859,12 @@ module ActionController #:nodoc:
0
       #     page.visual_effect :highlight, 'user_list'
0
       #   end
0
       #
0
-      # === Rendering with status and location headers
0
+      # === Rendering vanilla JavaScript
0
+      #
0
+      # In addition to using RJS with render :update, you can also just render vanilla JavaScript with :js.
0
+      #
0
+      #   # Renders "alert('hello')" and sets the mime type to text/javascript
0
+      #   render :js => "alert('hello')"
0
       #
0
       # All renders take the <tt>:status</tt> and <tt>:location</tt> options and turn them into headers. They can even be used together:
0
       #
0
@@ -898,6 +916,10 @@ module ActionController #:nodoc:
0
             response.content_type ||= Mime::XML
0
             render_for_text(xml.respond_to?(:to_xml) ? xml.to_xml : xml, options[:status])
0
 
0
+          elsif js = options[:js]
0
+            response.content_type ||= Mime::JS
0
+            render_for_text(js, options[:status])
0
+
0
           elsif json = options[:json]
0
             json = json.to_json unless json.is_a?(String)
0
             json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
...
184
185
186
 
 
 
 
187
188
189
...
844
845
846
 
 
 
 
 
 
847
848
849
...
184
185
186
187
188
189
190
191
192
193
...
848
849
850
851
852
853
854
855
856
857
858
859
0
@@ -184,6 +184,10 @@ class TestController < ActionController::Base
0
     render("test/hello")
0
   end
0
 
0
+  def render_vanilla_js_hello
0
+    render :js => "alert('hello')"
0
+  end
0
+
0
   def render_xml_hello
0
     @name = "David"
0
     render :template => "test/hello"
0
@@ -844,6 +848,12 @@ class RenderTest < Test::Unit::TestCase
0
     assert_equal "test", @response.body # name is explicitly set to 'test' inside the controller.
0
   end
0
 
0
+  def test_render_vanilla_js
0
+    get :render_vanilla_js_hello
0
+    assert_equal "alert('hello')", @response.body
0
+    assert_equal "text/javascript", @response.content_type
0
+  end
0
+
0
   def test_render_xml
0
     get :render_xml_hello
0
     assert_equal "<html>\n  <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *2.2.1 [RC2 or 2.2 final]*
0
 
0
+* Added render :js for people who want to render inline JavaScript replies without using RJS [DHH]
0
+
0
 * Fixed the option merging in Array#to_xml #1126 [Rudolf Gavlas]
0
 
0
 * Make I18n::Backend::Simple reload its translations in development mode [DHH/Sven Fuchs]

Comments