by
back

Generate an atom rss-feed with Ruby on Rails for a blog app

config/routes.rb

 get "/feed" => "pages#feed", :as => "feed"

app/controllers/pages_controller.rb

def feed
    @posts = Post.where(:active => true)
    respond_to do |format|
      format.atom { render layout: false }
    end
end

app/views/pages/feed.atom.builder

atom_feed {language: 'en-US', url: root_url} do |feed|
  feed.title "My own blog title"
  feed.updated @posts.maximum(:published_date)

  @posts.each do |post|
    feed.entry post, {published: post.published_at, updated: post.updated_at} do |entry|
      entry.title post.title
      entry.content post.text, type: 'html'
      entry.author do |author|
        author.name post.author_name
      end
      entry.url post_url(post)
      entry.summary post.summary
    end
  end
end

app/views/layouts/application.html.erb

<%= auto_discovery_link_tag(:rss, feed_url(:format => :atom)) %>


comments powered by Disqus