Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8956

Show
Ignore:
Timestamp:
02/29/08 18:09:23 (4 months ago)
Author:
david
Message:

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]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r8883 r8956  
    11*SVN* 
     2 
     3* 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] 
    24 
    35* Tests for distance_of_time_in_words with TimeWithZone instances. Closes #10914 [ernesto.jimenez] 
  • trunk/actionpack/lib/action_view/base.rb

    r8805 r8956  
    345345 
    346346    # symbolized version of the :format parameter of the request, or :html by default. 
     347    # 
     348    # EXCEPTION: If the :format parameter is not set, the Accept header will be examined for 
     349    # whether it contains the JavaScript mime type as its first priority. If that's the case, 
     350    # it will be used. This ensures that Ajax applications can use the same URL to support both 
     351    # JavaScript and non-JavaScript users. 
    347352    def template_format 
    348353      return @template_format if @template_format 
    349       format = controller && controller.respond_to?(:request) && controller.request.parameters[:format] 
    350       @template_format = format.blank? ? :html : format.to_sym 
     354 
     355      if controller && controller.respond_to?(:request) 
     356        parameter_format = controller.request.parameters[:format] 
     357        accept_format    = controller.request.accepts.first 
     358 
     359        case 
     360        when parameter_format.blank? && accept_format != :js 
     361          @template_format = :html 
     362        when parameter_format.blank? && accept_format == :js 
     363          @template_format = :js 
     364        else 
     365          @template_format = parameter_format.to_sym 
     366        end 
     367      else 
     368        @template_format = :html 
     369      end 
    351370    end 
    352371 
  • trunk/actionpack/test/controller/new_render_test.rb

    r8822 r8956  
    551551  end 
    552552 
     553  def test_render_with_default_from_accept_header 
     554    @request.env["HTTP_ACCEPT"] = "text/javascript" 
     555    get :greeting 
     556    assert_equal "$(\"body\").visualEffect(\"highlight\");", @response.body 
     557  end 
     558 
    553559  def test_render_rjs_with_default 
    554560    get :delete_with_js