2011-01-28 50 views

回答

114

首先,现在我推荐使用ATOM feed而不是RSS

ATOM feed的规格比RSS规范更具价值,包括国际化,内容类型和其他东西每个现代的阅读器都支持它。

约ATOM VS RSS

更多信息,可以发现:


博客帖子上到编码:

这个例子假设:

  • 模型称为NewsItem具有以下属性:
    • title
    • content
    • author_name
  • 该模型(news_items_controller.rb),到控制器您将添加feed ac和

我们将使用一个生成器模板和Ruby on Rails atom_feed helper这是非常有用的。

1的动作控制器

转到添加到app/controllers/news_items_controller.rb并添加:

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 => false } 

    # we want the RSS feed to redirect permanently to the ATOM feed 
    format.rss { redirect_to feed_path(:format => :atom), :status => :moved_permanently } 
    end 
end 

2.设置你的工具模板

现在,让我们添加模板打造饲料。

转到app/views/news_items/feed.atom.builder并添加:

atom_feed :language => '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 => '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 

3线它与路线

让我们提供的饲料在http://domain.com/feed

这将调用与ATOM行动格式,并将/feed.rss重定向到/feed.atom

转到config/routes.rb并添加:

resources :news_items 
match '/feed' => 'news_items#feed', 
     :as => :feed, 
     :defaults => { :format => 'atom' } 

4.添加链接Atom和RSS上的布局饲料

最后,所有剩下的是饲料添加到布局。

转到app/views/layouts/application.html.erb并添加这个您<head></head>部分:

<%= auto_discovery_link_tag :atom, "/feed" %> 
<%= auto_discovery_link_tag :rss, "/feed.rss" %> 

可能有一个错字或两个在,所以让我知道这对你的作品。

+1

要给予信贷,这是因为,这是我做的实施和使用基于关闭这个博客帖子和评论意见:http://lindsaar.net/2010/2/12/how -to-使-AN-RSS-饲料或原子饲料,在护栏 – tomeduarte 2011-01-28 20:14:28

+0

但为什么添加了一个全新的行动,而不是简单一个新的格式表示索引操作? – holden 2011-02-05 08:57:17

10

我做了类似但不创建一个新动作的东西。

index.atom.builder

atom_feed :language => 'en-US' do |feed| 
    feed.title "Articles" 
    feed.updated Time.now 

    @articles.each do |item| 
    next if item.published_at.blank? 

    feed.entry(item) do |entry| 
     entry.url article_url(item) 
     entry.title item.title 
     entry.content item.content, :type => 'html' 

     # the strftime is needed to work with Google Reader. 
     entry.updated(item.published_at.strftime("%Y-%m-%dT%H:%M:%SZ")) 
     entry.author item.user.handle 
    end 
    end 
end 

你不需要做任何特殊的控制器,除非你有一些特殊的代码,像我一样。例如,我使用will_paginate gem和atom feed,我不希望它分页,所以我这样做是为了避免这种情况。

控制器

def index 
    if current_user && current_user.admin? 
     @articles = Article.paginate :page => params[:page], :order => 'created_at DESC' 
    else 
     respond_to do |format| 
     format.html { @articles = Article.published.paginate :page => params[:page], :order => 'published_at DESC' } 
     format.atom { @articles = Article.published } 
     end 
    end 
    end