Generating RSS Feed for Podcast in Rails 5

The episode model has published method.

  def self.published
    where('published_at <= ?',

The index action in the episodes controller looks like this:

def index
  published_episodes = Episode.published
  respond_to do |format|
    format.html do
      @episodes = published_episodes
    format.rss do
      @episodes ='position desc')

Define routes.rb:

resources :episodes

Create index.rss.builder:

title = "Rails Screencasts"
author = "Bala Paranj"
description = "Learn practical tips and techniques on how to develop web applications using Ruby on Rails web framework. The episodes are short to minimize the amount of content to avoid overwhelming learners. The episodes are designed to give developers a quick way to get their feet wet and learn by building web apps."
keywords = "rails, ruby on rails, screencasts, podcasts, tips, tricks, tutorials, training, programming, railscast, web application framework"
ext = 'mp4'

#encoding: UTF-8

xml.instruct! :xml, :version => "1.0"

xml.rss :version => "2.0", "xmlns:itunes" => "",  "xmlns:media" => "", "xmlns:atom" => "" do do
    xml.tag!("atom:link",  "href"=>"", "rel"=>"self", "type"=>"application/rss+xml") 
    xml.title title ''
    xml.description description
    xml.language 'en'
    xml.pubDate @episodes.first.created_at.to_s(:rfc822)
    xml.lastBuildDate @episodes.first.created_at.to_s(:rfc822)
    xml.itunes :author, author
    xml.itunes :keywords, keywords
    xml.itunes :explicit, 'clean'
    xml.itunes :image, :href => ""
    xml.itunes :owner do
      xml.itunes :name, author
      xml.itunes :email, ''
    xml.itunes :block, 'no'
    xml.itunes :category, :text => 'Technology' do
      xml.itunes :category, :text => 'Software How-To'
    xml.itunes :category, :text => 'Education' do
      xml.itunes :category, :text => 'Training'

    @episodes.each do  |episode|
      xml.item do
        xml.title episode.title
        xml.description episode.description
        xml.pubDate episode.created_at.to_s(:rfc822)
        xml.enclosure :url => "{episode.file_name}", :length => episode.file_size, :type => 'video/mp4' episode_url(episode)
        xml.guid({:isPermaLink => "false"}, episode.permalink)
        xml.itunes :author, author
        xml.itunes :subtitle, truncate(episode.description, :length => 150)
        xml.itunes :summary, episode.description
        xml.itunes :explicit, 'no'
        xml.itunes :duration, episode.duration

You can see this in action at RubyPlus episodes page. You will be able to subscribe to the screencast on iTunes.

Related Articles

Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.