Carla Dessi
Carla Dessi

Reputation: 9666

Creating a RSS feed with Rails

I'm trying to create an RSS feed for my news posts, I've googled it and come up with this code:

def feed
  @posts = News.all(:conditions => "#{Settings.show} = 1", :select => "id, title, heading, content, date_posted", :order => "date_posted DESC") 

  respond_to do |format|
    format.rss { render :layout => false }
  end
end

then in a file called "feed.rss.builder" I have this:

xml.instruct! :xml, :version => "1.0" 
xml.rss :version => "2.0" do
  xml.channel do
    xml.title "Your Blog Title"
    xml.description "A blog about software and chocolate"
    xml.link posts_url

    for post in @posts
      xml.item do
        xml.title post.title
        xml.description post.content
        xml.pubDate post.date_posted.to_s(:rfc822)
        xml.link post_url(post)
        xml.guid post_url(post)
       end
    end
  end
end

I've added it into my routes file match "/news/feed" => "aboutus#feed" but when I go to that page nothing is rendered..

Upvotes: 2

Views: 2700

Answers (2)

Carla Dessi
Carla Dessi

Reputation: 9666

This was the code I ended up with:

def news

@news = News.find(:all, :order => "date_posted desc", :conditions => "#{Settings.show} = 1")
render :template => 'about-us/news/feed.rss.builder', :layout => false

end

and:

xml.instruct! :xml, :version => "1.0" 
xml.rss :version => "2.0" do
  xml.channel do
    xml.title "News"
    xml.description "Description"
    xml.link "/news"

for post in @news
  xml.item do
    xml.title post.title
    xml.description post.content
    xml.pubDate post.date_posted.to_s(:rfc822)
    xml.link "/news/#{post.reference}"
    xml.guid "/news/#{post.reference}"
    xml.icon "/favicon.ico"
   end
end
end
end

Upvotes: 5

matkins
matkins

Reputation: 1991

Is it possible that it's working correctly, but that your browser isn't displaying the RSS?

You could try using curl in terminal to see if anything is being rendered:

curl http://localhost:3000/news/feed

Upvotes: 1

Related Questions