removes the RJS template handler
This commit is contained in:
parent
eea66892c8
commit
5850f16935
@ -33,10 +33,10 @@ module ClassMethods
|
||||
# and all actions except <tt>:edit</tt> respond to <tt>:xml</tt> and
|
||||
# <tt>:json</tt>.
|
||||
#
|
||||
# respond_to :rjs, :only => :create
|
||||
# respond_to :json, :only => :create
|
||||
#
|
||||
# This specifies that the <tt>:create</tt> action and no other responds
|
||||
# to <tt>:rjs</tt>.
|
||||
# to <tt>:json</tt>.
|
||||
def respond_to(*mimes)
|
||||
options = mimes.extract_options!
|
||||
|
||||
@ -106,8 +106,8 @@ def clear_respond_to
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# If the client wants HTML, we just redirect them back to the person list. If they want Javascript
|
||||
# (format.js), then it is an RJS request and we render the RJS template associated with this action.
|
||||
# If the client wants HTML, we just redirect them back to the person list. If they want JavaScript,
|
||||
# then it is an Ajax request and we render the JavaScript template associated with this action.
|
||||
# Lastly, if the client wants XML, we render the created person as XML, but with a twist: we also
|
||||
# include the person's company in the rendered XML, so you get something like this:
|
||||
#
|
||||
|
@ -3,12 +3,10 @@ module ActionView #:nodoc:
|
||||
class Template
|
||||
module Handlers #:nodoc:
|
||||
autoload :ERB, 'action_view/template/handlers/erb'
|
||||
autoload :RJS, 'action_view/template/handlers/rjs'
|
||||
autoload :Builder, 'action_view/template/handlers/builder'
|
||||
|
||||
def self.extended(base)
|
||||
base.register_default_template_handler :erb, ERB.new
|
||||
base.register_template_handler :rjs, RJS.new
|
||||
base.register_template_handler :builder, Builder.new
|
||||
end
|
||||
|
||||
|
@ -1,13 +0,0 @@
|
||||
module ActionView
|
||||
module Template::Handlers
|
||||
class RJS
|
||||
# Default format used by RJS.
|
||||
class_attribute :default_format
|
||||
self.default_format = Mime::JS
|
||||
|
||||
def call(template)
|
||||
"update_page do |page|;#{template.source}\nend"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -713,17 +713,10 @@ def html_fragment_cached_with_partial
|
||||
end
|
||||
end
|
||||
|
||||
def js_fragment_cached_with_partial
|
||||
respond_to do |format|
|
||||
format.js
|
||||
end
|
||||
end
|
||||
|
||||
def formatted_fragment_cached
|
||||
respond_to do |format|
|
||||
format.html
|
||||
format.xml
|
||||
format.js
|
||||
end
|
||||
end
|
||||
|
||||
@ -770,13 +763,6 @@ def test_render_inline_before_fragment_caching
|
||||
assert_match("Some cached content", @store.read('views/test.host/functional_caching/inline_fragment_cached'))
|
||||
end
|
||||
|
||||
def test_fragment_caching_in_rjs_partials
|
||||
xhr :get, :js_fragment_cached_with_partial
|
||||
assert_response :success
|
||||
assert_match(/Old fragment caching in a partial/, @response.body)
|
||||
assert_match("Old fragment caching in a partial", @store.read('views/test.host/functional_caching/js_fragment_cached_with_partial'))
|
||||
end
|
||||
|
||||
def test_html_formatted_fragment_caching
|
||||
get :formatted_fragment_cached, :format => "html"
|
||||
assert_response :success
|
||||
|
@ -35,9 +35,6 @@ def render_default_for_erb
|
||||
def render_default_for_builder
|
||||
end
|
||||
|
||||
def render_default_for_rjs
|
||||
end
|
||||
|
||||
def render_change_for_builder
|
||||
response.content_type = Mime::HTML
|
||||
render :action => "render_default_for_builder"
|
||||
@ -129,12 +126,6 @@ def test_default_for_builder
|
||||
assert_equal "utf-8", @response.charset
|
||||
end
|
||||
|
||||
def test_default_for_rjs
|
||||
xhr :post, :render_default_for_rjs
|
||||
assert_equal Mime::JS, @response.content_type
|
||||
assert_equal "utf-8", @response.charset
|
||||
end
|
||||
|
||||
def test_change_for_builder
|
||||
get :render_change_for_builder
|
||||
assert_equal Mime::HTML, @response.content_type
|
||||
|
@ -73,13 +73,12 @@ def just_xml
|
||||
def using_defaults
|
||||
respond_to do |type|
|
||||
type.html
|
||||
type.js
|
||||
type.xml
|
||||
end
|
||||
end
|
||||
|
||||
def using_defaults_with_type_list
|
||||
respond_to(:html, :js, :xml)
|
||||
respond_to(:html, :xml)
|
||||
end
|
||||
|
||||
def made_for_content_type
|
||||
@ -130,7 +129,6 @@ def handle_any_any
|
||||
def all_types_with_layout
|
||||
respond_to do |type|
|
||||
type.html
|
||||
type.js
|
||||
end
|
||||
end
|
||||
|
||||
@ -299,11 +297,6 @@ def test_using_defaults
|
||||
assert_equal "text/html", @response.content_type
|
||||
assert_equal 'Hello world!', @response.body
|
||||
|
||||
@request.accept = "text/javascript"
|
||||
get :using_defaults
|
||||
assert_equal "text/javascript", @response.content_type
|
||||
assert_equal '$("body").visualEffect("highlight");', @response.body
|
||||
|
||||
@request.accept = "application/xml"
|
||||
get :using_defaults
|
||||
assert_equal "application/xml", @response.content_type
|
||||
@ -316,11 +309,6 @@ def test_using_defaults_with_type_list
|
||||
assert_equal "text/html", @response.content_type
|
||||
assert_equal 'Hello world!', @response.body
|
||||
|
||||
@request.accept = "text/javascript"
|
||||
get :using_defaults_with_type_list
|
||||
assert_equal "text/javascript", @response.content_type
|
||||
assert_equal '$("body").visualEffect("highlight");', @response.body
|
||||
|
||||
@request.accept = "application/xml"
|
||||
get :using_defaults_with_type_list
|
||||
assert_equal "application/xml", @response.content_type
|
||||
@ -428,13 +416,6 @@ def test_browser_check_with_any_any
|
||||
assert_equal 'HTML', @response.body
|
||||
end
|
||||
|
||||
|
||||
def test_rjs_type_skips_layout
|
||||
@request.accept = "text/javascript"
|
||||
get :all_types_with_layout
|
||||
assert_equal 'RJS for all_types_with_layout', @response.body
|
||||
end
|
||||
|
||||
def test_html_type_with_layout
|
||||
@request.accept = "text/html"
|
||||
get :all_types_with_layout
|
||||
@ -444,9 +425,6 @@ def test_html_type_with_layout
|
||||
def test_xhr
|
||||
xhr :get, :js_or_html
|
||||
assert_equal 'JS', @response.body
|
||||
|
||||
xhr :get, :using_defaults
|
||||
assert_equal '$("body").visualEffect("highlight");', @response.body
|
||||
end
|
||||
|
||||
def test_custom_constant
|
||||
@ -643,11 +621,6 @@ def teardown
|
||||
end
|
||||
|
||||
def test_using_resource
|
||||
@request.accept = "text/javascript"
|
||||
get :using_resource
|
||||
assert_equal "text/javascript", @response.content_type
|
||||
assert_equal '$("body").visualEffect("highlight");', @response.body
|
||||
|
||||
@request.accept = "application/xml"
|
||||
get :using_resource
|
||||
assert_equal "application/xml", @response.content_type
|
||||
|
@ -23,8 +23,7 @@ class ImpliedController < ActionController::Base
|
||||
"content_type/implied/i_am_html_erb.html.erb" => "Hello world!",
|
||||
"content_type/implied/i_am_xml_erb.xml.erb" => "<xml>Hello world!</xml>",
|
||||
"content_type/implied/i_am_html_builder.html.builder" => "xml.p 'Hello'",
|
||||
"content_type/implied/i_am_xml_builder.xml.builder" => "xml.awesome 'Hello'",
|
||||
"content_type/implied/i_am_js_rjs.js.rjs" => "page.alert 'hello'"
|
||||
"content_type/implied/i_am_xml_builder.xml.builder" => "xml.awesome 'Hello'"
|
||||
)]
|
||||
end
|
||||
|
||||
@ -93,12 +92,6 @@ class ImpliedContentTypeTest < Rack::TestCase
|
||||
|
||||
assert_header "Content-Type", "application/xml; charset=utf-8"
|
||||
end
|
||||
|
||||
test "sets Content-Type as text/javascript when rendering *.js" do
|
||||
get "/content_type/implied/i_am_js_rjs", "format" => "js"
|
||||
|
||||
assert_header "Content-Type", "text/javascript; charset=utf-8"
|
||||
end
|
||||
end
|
||||
|
||||
class ExplicitCharsetTest < Rack::TestCase
|
||||
|
@ -70,8 +70,8 @@ class LayoutOptionsTest < Rack::TestCase
|
||||
class MismatchFormatController < ::ApplicationController
|
||||
self.view_paths = [ActionView::FixtureResolver.new(
|
||||
"layouts/application.html.erb" => "<html><%= yield %></html>",
|
||||
"controller_layouts/mismatch_format/index.js.rjs" => "page[:test].ext",
|
||||
"controller_layouts/mismatch_format/implicit.rjs" => "page[:test].ext"
|
||||
"controller_layouts/mismatch_format/index.xml.builder" => "xml.instruct!",
|
||||
"controller_layouts/mismatch_format/implicit.builder" => "xml.instruct!"
|
||||
)]
|
||||
|
||||
def explicit
|
||||
@ -81,15 +81,17 @@ def explicit
|
||||
|
||||
class MismatchFormatTest < Rack::TestCase
|
||||
testing ControllerLayouts::MismatchFormatController
|
||||
|
||||
XML_INSTRUCT = %Q(<?xml version="1.0" encoding="UTF-8"?>\n)
|
||||
|
||||
test "if JS is selected, an HTML template is not also selected" do
|
||||
get :index, "format" => "js"
|
||||
assert_response "$(\"test\").ext();"
|
||||
test "if XML is selected, an HTML template is not also selected" do
|
||||
get :index, :format => "xml"
|
||||
assert_response XML_INSTRUCT
|
||||
end
|
||||
|
||||
test "if JS is implicitly selected, an HTML template is not also selected" do
|
||||
test "if XML is implicitly selected, an HTML template is not also selected" do
|
||||
get :implicit
|
||||
assert_response "$(\"test\").ext();"
|
||||
assert_response XML_INSTRUCT
|
||||
end
|
||||
|
||||
test "if an HTML template is explicitly provides for a JS template, an error is raised" do
|
||||
|
@ -1,6 +0,0 @@
|
||||
page.assign 'title', 'Hey'
|
||||
cache do
|
||||
page['element_1'].visual_effect :highlight
|
||||
page['element_2'].visual_effect :highlight
|
||||
end
|
||||
page.assign 'footer', 'Bye'
|
@ -1 +0,0 @@
|
||||
page.replace_html 'notices', :partial => 'partial'
|
@ -1 +0,0 @@
|
||||
page.alert 'hello world!'
|
@ -1 +0,0 @@
|
||||
page << "RJS for all_types_with_layout"
|
@ -1 +0,0 @@
|
||||
page[:body].visual_effect :highlight
|
@ -1 +0,0 @@
|
||||
page[:body].visual_effect :highlight
|
@ -1 +0,0 @@
|
||||
page[:body].visual_effect :highlight
|
@ -3,14 +3,14 @@ h2. AJAX on Rails
|
||||
This guide covers the built-in Ajax/JavaScript functionality of Rails (and more); it will enable you to create rich and dynamic AJAX applications with ease! We will cover the following topics:
|
||||
|
||||
* Quick introduction to AJAX and related technologies
|
||||
* Handling JavaScript the Rails way: Rails helpers, RJS, Prototype and script.aculo.us
|
||||
* Handling JavaScript the Rails way: Rails helpers, Prototype and script.aculo.us
|
||||
* Testing JavaScript functionality
|
||||
|
||||
endprologue.
|
||||
|
||||
h3. Hello AJAX - a Quick Intro
|
||||
|
||||
If you are a 'show me the code' type of person, you might want to skip this part and jump to the RJS section right away. However, I would really recommend to read it - you'll need the basics of DOM, http requests and other topics discussed here to really understand Ajax on Rails.
|
||||
You'll need the basics of DOM, HTTP requests and other topics discussed here to really understand Ajax on Rails.
|
||||
|
||||
h4. Asynchronous JavaScript + XML
|
||||
|
||||
@ -62,7 +62,7 @@ link_to_remote "Add to cart",
|
||||
|
||||
* The second parameter, the +options+ hash is the most interesting part as it has the AJAX specific stuff:
|
||||
** *:url* This is the only parameter that is always required to generate the simplest remote link (technically speaking, it is not required, you can pass an empty +options+ hash to +link_to_remote+ - but in this case the URL used for the POST request will be equal to your current URL which is probably not your intention). This URL points to your AJAX action handler. The URL is typically specified by Rails REST view helpers, but you can use the +url_for+ format too.
|
||||
** *:update* There are basically two ways of injecting the server response into the page: One is involving RJS and we will discuss it in the next chapter, and the other is specifying a DOM id of the element we would like to update. The above example demonstrates the simplest way of accomplishing this - however, we are in trouble if the server responds with an error message because that will be injected into the page too! However, Rails has a solution for this situation:
|
||||
** *:update* Specifying a DOM id of the element we would like to update. The above example demonstrates the simplest way of accomplishing this - however, we are in trouble if the server responds with an error message because that will be injected into the page too! However, Rails has a solution for this situation:
|
||||
|
||||
<ruby>
|
||||
link_to_remote "Add to cart",
|
||||
@ -178,7 +178,7 @@ h5. +remote_function+
|
||||
|
||||
h5. +update_page+
|
||||
|
||||
h4. JavaScript without RJS
|
||||
h4. Serving JavaScript
|
||||
|
||||
First we'll check out how to send JavaScript to the server manually. You are practically never going to need this, but it's interesting to understand what's going on under the hood.
|
||||
|
||||
@ -193,15 +193,6 @@ end
|
||||
|
||||
What happens here is that by specifying the Content-Type header variable, we instruct the browser to evaluate the text we are sending over (rather than displaying it as plain text, which is the default behavior).
|
||||
|
||||
h4. RJS Templates
|
||||
|
||||
If you don't want to clutter your controllers with view code (especially when your inline RJS is more than a few lines), you can move your RJS code to a template file. RJS templates should go to the +/app/views/+ directory, just as +.html.erb+ or any other view files of the appropriate controller, conventionally named +js.rjs+.
|
||||
|
||||
To rewrite the above example, you can leave the body of the action empty, and create a RJS template named +javascript_test.js.rjs+, containing the following line:
|
||||
|
||||
<ruby>
|
||||
page.alert "Hello from inline RJS"
|
||||
</ruby>
|
||||
|
||||
h3. Testing JavaScript
|
||||
|
||||
|
@ -90,7 +90,7 @@ If we want to display the properties of all the books in our view, we can do so
|
||||
<%= link_to 'New book', new_book_path %>
|
||||
</ruby>
|
||||
|
||||
NOTE: The actual rendering is done by subclasses of +ActionView::TemplateHandlers+. This guide does not dig into that process, but it's important to know that the file extension on your view controls the choice of template handler. In Rails 2, the standard extensions are +.erb+ for ERB (HTML with embedded Ruby), +.rjs+ for RJS (JavaScript with embedded ruby) and +.builder+ for Builder (XML generator).
|
||||
NOTE: The actual rendering is done by subclasses of +ActionView::TemplateHandlers+. This guide does not dig into that process, but it's important to know that the file extension on your view controls the choice of template handler. In Rails 2, the standard extensions are +.erb+ for ERB (HTML with embedded Ruby), and +.builder+ for Builder (XML generator).
|
||||
|
||||
h4. Using +render+
|
||||
|
||||
@ -284,7 +284,7 @@ TIP: You don't need to call +to_xml+ on the object that you want to render. If y
|
||||
|
||||
h5. Rendering Vanilla JavaScript
|
||||
|
||||
Rails can render vanilla JavaScript (as an alternative to using +update+ with an +.rjs+ file):
|
||||
Rails can render vanilla JavaScript:
|
||||
|
||||
<ruby>
|
||||
render :js => "alert('Hello Rails');"
|
||||
|
Loading…
Reference in New Issue
Block a user