Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, nowadays <strong>I recommend using an ATOM feed instead of RSS</strong>.</p> <p>The specification of ATOM feed offers more value than the RSS one with internationalization, content types and other things <em>and</em> every modern feed reader supports it.</p> <p>More info about ATOM vs RSS can be found at:</p> <ul> <li>the <a href="http://en.wikipedia.org/wiki/Atom_(standard)#Atom_compared_to_RSS_2.0" rel="noreferrer">Wikipedia ATOM entry</a></li> <li><a href="http://www.problogger.net/archives/2006/03/30/rss-vs-atom-whats-the-big-deal/" rel="noreferrer">PRO Blogger</a> and <a href="http://www.freemarketingzone.com/rss/atom-vs-rss.html" rel="noreferrer">Free Marketing Zone</a> blog posts about the subject</li> </ul> <hr> <p><strong>On to the coding:</strong></p> <p>This example assumes:</p> <ul> <li>a model called <code>NewsItem</code> with the following attributes: <ul> <li><code>title</code></li> <li><code>content</code></li> <li><code>author_name</code></li> </ul></li> <li>a controller for that model (<code>news_items_controller.rb</code>), to which you'll add the <code>feed</code> action</li> </ul> <p>We'll use a builder template for this and the Ruby on Rails <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/AtomFeedHelper.html" rel="noreferrer">atom_feed helper</a> which is of great use. </p> <p><strong>1. Add the action to the controller</strong></p> <p>Go to <code>app/controllers/news_items_controller.rb</code> and add:</p> <pre><code>def feed # this will be the name of the feed displayed on the feed reader @title = "FEED title" # the news items @news_items = NewsItem.order("updated_at desc") # this will be our Feed's update timestamp @updated = @news_items.first.updated_at unless @news_items.empty? respond_to do |format| format.atom { render :layout =&gt; false } # we want the RSS feed to redirect permanently to the ATOM feed format.rss { redirect_to feed_path(:format =&gt; :atom), :status =&gt; :moved_permanently } end end </code></pre> <p><strong>2. Setup your builder template</strong></p> <p>Now let's add the template to build the feed.</p> <p>Go to <code>app/views/news_items/feed.atom.builder</code> and add:</p> <pre><code>atom_feed :language =&gt; 'en-US' do |feed| feed.title @title feed.updated @updated @news_items.each do |item| next if item.updated_at.blank? feed.entry( item ) do |entry| entry.url news_item_url(item) entry.title item.title entry.content item.content, :type =&gt; 'html' # the strftime is needed to work with Google Reader. entry.updated(item.updated_at.strftime("%Y-%m-%dT%H:%M:%SZ")) entry.author do |author| author.name entry.author_name end end end end </code></pre> <p><strong>3. Wire it up with a route</strong></p> <p>Let's make the feeds available at <a href="http://domain.com/feed" rel="noreferrer">http://domain.com/feed</a></p> <p>This will call the action with the ATOM format by default and redirect <code>/feed.rss</code> to <code>/feed.atom</code>.</p> <p>Go to <code>config/routes.rb</code> and add:</p> <pre><code>resources :news_items match '/feed' =&gt; 'news_items#feed', :as =&gt; :feed, :defaults =&gt; { :format =&gt; 'atom' } </code></pre> <p><strong>4. Add the link to ATOM and RSS feeds on the layout</strong></p> <p>Finally, all that is left is to add the feed to the layout.</p> <p>Go to <code>app/views/layouts/application.html.erb</code> and add this your <code>&lt;head&gt;&lt;/head&gt;</code> section:</p> <pre><code>&lt;%= auto_discovery_link_tag :atom, "/feed" %&gt; &lt;%= auto_discovery_link_tag :rss, "/feed.rss" %&gt; </code></pre> <hr> <p>There may be a typo or two in that, so let me know if this works for you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload