Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Explicitly setting the content type get overriden by respond_to / render

#1783

When doing the following, the content type which is explicitly set is being overridden.

class Admin::NotificationsController < Admin::AdminController

def show

@notification = Notification.find(params[:id])
respond_to do |format|
  format.html do
    content = render_to_string :layout => false,
                               :template => 'notification_mailer/notify.text.html.rhtml'
    content = "<head><title>Subject: #{@notification.subject}</title></head>#{content}"
    render :text => content, :content_type => :html
  end
  format.txt do
    content = render_to_string :layout => false,
                               :template => 'notification_mailer/notify.text.plain.rhtml'
    content = "Subject: #{@notification.subject}\n\n\n#{content}"
    render :text => content, :content_type => :text
  end
end

end

end

class Admin::NotificationsControllerTest < ActionController::TestCase def test_html_output_from_html_method

@request.env['HTTP_ACCEPT'] = "text/html"
get(:show, {:format => 'html', :id => "1"}, session_for_user(:admin_user) )
assert_response(:success)
assert_tag :tag => "table"
assert_equal("text/html; charset=utf-8", @response.headers["type"])

end

def test_html_not_output_from_plain_method

get( :show, {:format => 'txt', :id => "1"}, session_for_user(:admin_user) )
assert_response(:success)
assert_no_tag :tag => "html"
assert_equal("text/plain; charset=utf-8", @response.headers["type"])

end end

1) Failure: test_html_not_output_from_plain_method(Admin::NotificationsControllerTest)

[test/functional/admin/notifications_controller_test.rb:24:in `test_html_not_output_from_plain_method'
 /shared/home/mike.gaffney/workspace/git-ruby/vendor/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:94:in `__send__'
 /shared/home/mike.gaffney/workspace/git-ruby/vendor/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:94:in `run']:

<"text/plain; charset=utf-8"> expected but was <"text; charset=utf-8">.

2) Failure: test_html_output_from_html_method(Admin::NotificationsControllerTest)

[test/functional/admin/notifications_controller_test.rb:17:in `test_html_output_from_html_method'
 /shared/home/mike.gaffney/workspace/git-ruby/vendor/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:94:in `__send__'
 /shared/home/mike.gaffney/workspace/git-ruby/vendor/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:94:in `run']:

<"text/html; charset=utf-8"> expected but was <"html; charset=utf-8">.

Reported by gaffo · January 20th, 2009 @ 03:51 AM

State: resolved
Milestone: 3.0.2
Assigned to: Yehuda Katz (wycats) Yehuda Katz (wycats)
Importance: none

Activity

  1. Michael Koziarski
  2. Michael Koziarski
  3. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from 2.2, 2.3 to 2.2, 2.3, bugmash

    August 7th, 2009 @ 01:56 PM

  4. Pratik
    Pratik

    Adding Jose to notifications as he has dabbled with this code recently.

    August 8th, 2009 @ 02:53 AM

  5. José Valim
    José Valim

    Sorry, but I don't think the content-type is being overwritten, it just does not accept symbols. So it's just converting a symbol to string (that's why you get "html" in the response).

    Can you please try doing this:

      render :text => content, :content_type => Mime::HTML.to_s
    
      render :text => content, :content_type => Mime::TEXT.to_s
    

    (it will probably work without .to_s also)

    August 8th, 2009 @ 09:04 AM

  6. Michael Koziarski
    Michael Koziarski

    It should probably accept all three, symbol, string and Mime::CONSTANT.

    August 8th, 2009 @ 11:25 AM

  7. Felipe Talavera
    Felipe Talavera

    I've been working and discussing with José Valim and finnally I came with a patch to force the symbol to be converted to Mime::Type in render method into ActiveControler::base class. The patch is for the 2-3-stable branch, because in the master all the controller works in another way.

    I've attached a patch.

    August 9th, 2009 @ 08:08 PM

  8. José Valim
    José Valim

    Felipe, thanks for the patch. However, we have the same code in actioncontroller/streaming.rb, so would be desirable we refactor both into a single method.

    August 9th, 2009 @ 10:10 PM

  9. José Valim
    José Valim
    • Assigned user changed from Michael Koziarski to Yehuda Katz (wycats)
    • State changed from new to incomplete
    • Milestone cleared.

    Assigning to Yehuda, he is the guy to handle it on both 2.3 and 3.0.

    August 9th, 2009 @ 11:10 PM

  10. José Valim
    José Valim
    • Tag changed from 2.2, 2.3, bugmash to 2.2, 2.3

    August 9th, 2009 @ 11:23 PM

  11. Yehuda Katz (wycats)
    Yehuda Katz (wycats)
    • State changed from incomplete to resolved

    February 24th, 2010 @ 07:17 AM

  12. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to

    October 15th, 2010 @ 11:01 PM

  13. Dave Myron
    Dave Myron

    Was this fixed in 3.0.2? It appears that, with 3.0.3, :content_type => :html still sets the Content-Type to "html" instead of "text/html".

    January 7th, 2011 @ 07:40 AM