This project is archived and is in readonly mode.

Explicitly setting the content type get overriden by respond_to / render
Reported by gaffo | January 20th, 2009 @ 03:51 AM | in 3.0.2
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">.
Comments and changes to this ticket
- 
         Michael Koziarski January 20th, 2009 @ 03:53 AM- Milestone cleared.
 
- 
         Michael Koziarski June 9th, 2009 @ 08:40 AM- Milestone set to 2.x
 
- 
         CancelProfileIsBroken August 7th, 2009 @ 01:56 PM- Tag changed from 2.2, 2.3 to 2.2, 2.3, bugmash
 
- 
         Pratik August 8th, 2009 @ 02:53 AMAdding Jose to notifications as he has dabbled with this code recently. 
- 
         José Valim August 8th, 2009 @ 09:04 AMSorry, 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_srender :text => content, :content_type => Mime::TEXT.to_s(it will probably work without .to_s also) 
- 
         Michael Koziarski August 8th, 2009 @ 11:25 AMIt should probably accept all three, symbol, string and Mime::CONSTANT. 
- 
            
         Felipe Talavera August 9th, 2009 @ 08:08 PMI'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. 
- 
         José Valim August 9th, 2009 @ 10:10 PMFelipe, 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. 
- 
         José Valim August 9th, 2009 @ 11:10 PM- 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. 
- 
         José Valim August 9th, 2009 @ 11:23 PM- Tag changed from 2.2, 2.3, bugmash to 2.2, 2.3
 
- 
         Yehuda Katz (wycats) February 24th, 2010 @ 07:17 AM- State changed from incomplete to resolved
 
- 
         Jeremy Kemper October 15th, 2010 @ 11:01 PM- Milestone set to 3.0.2
- Importance changed from  to 
 
- 
            
         Dave Myron January 7th, 2011 @ 07:40 AMWas this fixed in 3.0.2? It appears that, with 3.0.3, :content_type => :htmlstill sets the Content-Type to "html" instead of "text/html".
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
 CancelProfileIsBroken
      CancelProfileIsBroken
 gaffo
      gaffo
 José Valim
      José Valim
 Michael Koziarski
      Michael Koziarski
 Pratik
      Pratik
 Yehuda Katz (wycats)
      Yehuda Katz (wycats)