Search code examples
ruby-on-rails-3herokurssmime-typesscraper

Rails - render :content_type has no effect


I'm developing a Ruby/Rails app which scrapes another website and renders an RSS feed with the data.

Because this app is built on Heroku, I am generating the RSS feed via a controller, rather than dumping it to the file-system and serving it as an asset.

However when I set the render :content_type hash, there is no effect. The response's content type remains text/plain.

news action on FeedController

def news
  do_scrape
  @posts = UbuEntry.all(:order => "created_at DESC", :limit => 400)
  render :layout => false, :content_type => Mime::RSS
end

Full routes.rb

UbuWebRSS::Application.routes.draw do
  root :to => 'index#index'
  scope :format => true, :constraints => { :format => 'rss' } do
    get 'feed/news' => 'feed#news'
  end
end

You can see the live outcome here:

http://www.ubuwebrss.com/feed/news.rss

And the full source code:

https://github.com/freen/ubuwebrss/

I've looked around on Google and StackOverflow but it's not clear what I'm doing wrong.

Any tips? Thanks!

FYI:

The view script: /app/views/feed/news.rss.builder:

xml.instruct! :xml, :version => "1.0" 
xml.rss :version => "2.0" do
  xml.channel do
    xml.title "UbuWeb"
    xml.description "UbuWeb is a completely independent resource dedicated to all strains of the avant-garde, ethnopoetics, and outsider arts."
    xml.link 'http://www.ubu.com'

    for post in @posts
      xml.item do
        xml.title post.title

        description = post.description.to_s
        unless description.empty?
          # Manually add description for custom HTML sanitizing
          description = @sanitizer_basic.clean(description)
          xml << " " * 6
          xml << "<description><![CDATA[" + description + "]]></description>\n"
        end

        xml.pubDate post.created_at.to_s(:rfc822)
        xml.link post.href
        xml.guid post.href
      end
    end
  end
end

Solution

  • Seems like it's returning the correct content type here (with curl -i <url>):

    HTTP/1.1 200 OK 
    Server: nginx
    Date: Mon, 04 Feb 2013 00:10:05 GMT
    Content-Type: application/rss+xml; charset=utf-8
    Content-Length: 121890
    Connection: keep-alive
    X-Ua-Compatible: IE=Edge,chrome=1
    Etag: "74ebbfe3182fef13d8a737580453f688"
    Cache-Control: max-age=0, private, must-revalidate
    X-Request-Id: 9377e469ffad158b2480a3f6b2f2866c
    X-Runtime: 0.748709
    X-Rack-Cache: miss
    

    Could possibly be a bug in Chrome rather than your app.