Codingfish

How to create an RSS Feed with Rails 4 in 3 Steps

Published: December 07, 2014    Author: Achim Fischer
  Tweet      Share      Share      Pin it   
My website has recently been relaunched. Now it uses Chimoora, a Ruby on Rails based Content Management System (CMS). I was just setting up a new RSS Reader when I realized that Chimoora still had no RSS Feed for the blog articles.

Here is how I implemented the RSS Feed for Chimoora in 3 steps.






Step 1

Create a new method in the blog articles controller.

Edit
app/controllers/blog_articles_controller.rb

Add
  def feed
    @blog_articles = BlogArticle.all
    respond_to do |format|
      format.rss { render :layout => false }
    end
  end



Step 2

Add a "view" for the feed.

Create the file
app/views/blog_articles/feed.rss.builder

Add this code
#encoding: UTF-8

xml.instruct! :xml, :version => "1.0"
xml.rss :version => "2.0" do
  xml.channel do
    xml.title "Codingfish"
    xml.author "Achim Fischer"
    xml.description "Software-Development, Mobiles Devices, Photography"
    xml.link "https://www.codingfish.com"
    xml.language "en"

    for article in @blog_articles
      xml.item do
        if article.title
          xml.title article.title
        else
          xml.title ""
        end
        xml.author "Achim Fischer"
        xml.pubDate article.created_at.to_s(:rfc822)
        xml.link "https://www.codingfish.com/blog/" + article.id.to_s + "-" + article.alias
        xml.guid article.id

        text = article.text
		# if you like, do something with your content text here e.g. insert image tags.
		# Optional. I'm doing this on my website.
        if article.image.exists?
            image_url = article.image.url(:large)
            image_caption = article.image_caption
            image_align = ""
            image_tag = "
                <p><img src='" + image_url +  "' alt='" + image_caption + "' title='" + image_caption + "' align='" + image_align  + "' /></p>
              "
            text = text.sub('{image}', image_tag)
        end
        xml.description "<p>" + text + "</p>"

      end
    end
  end
end



Step 3

Add a route to /feed in the router file.

Edit
config/routes.rb

Add
get 'feed' => 'blog_articles#feed'



Optional

Add the following entry in the head section of your site template.

Edit
views/layouts/application.html.erb

Add a line like
<link rel="alternate" type="application/rss+xml" title="Codingfish RSS Feed" href="https://www.codingfish.com/feed?format=rss">



That's it :-)

The new RSS Feed is now available under www.codingfish.com/feed?format=rss

Filed in Categories

Chimoora   Rails  

About the author


Achim
Founder and Managing Director of Codingfish. Android- and Rails-developer. Creator of the CMS Chimoora. Big fan of mobile devices. Hobby photographer. Feel free to add me on: Twitter, Facebook, Google+ and LinkedIn






© 2007-2017 Achim Fischer, All Rights Reserved - Website powered by Chimoora
Android and Google Play are trademarks of Google Inc.
The Android robot is reproduced or modified from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.