在 Rails 3 中生成 RSS 提要 [英] Generating RSS feed in Rails 3

查看:46
本文介绍了在 Rails 3 中生成 RSS 提要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在 Rails 3 中生成提要的最佳实践/标准模式.是 http://railscasts.com/episodes/87-generate-rss-feeds 仍然有效吗?

I'm looking for a best practice/standard pattern for generating feeds in Rails 3. Is http://railscasts.com/episodes/87-generating-rss-feeds still valid?

推荐答案

首先,现在我建议使用 ATOM 提要而不是 RSS.

在国际化、内容类型和其他方面,ATOM 提要的规范比 RSS 提供了更多价值每个现代提要阅读器都支持它.

The specification of ATOM feed offers more value than the RSS one with internationalization, content types and other things and every modern feed reader supports it.

有关 ATOM 与 RSS 的更多信息,请访问:

More info about ATOM vs RSS can be found at:

  • the Wikipedia ATOM entry
  • PRO Blogger and Free Marketing Zone blog posts about the subject

关于编码:

这个例子假设:

  • 一个名为 NewsItem 的模型,具有以下属性:
    • title
    • 内容
    • author_name
    • a model called NewsItem with the following attributes:
      • title
      • content
      • author_name

      我们将为此使用构建器模板和 Ruby on Rails atom_feed helper 这很有用.

      We'll use a builder template for this and the Ruby on Rails atom_feed helper which is of great use.

      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.设置您的构建器模板

      现在让我们添加模板来构建提要.

      Now let's add the template to build the feed.

      转到 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.

      This will call the action with the ATOM format by default and redirect /feed.rss to /feed.atom.

      转到 config/routes.rb 并添加:

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

      4.在布局上添加指向 ATOM 和 RSS 提要的链接

      最后,剩下的就是将提要添加到布局中.

      Finally, all that is left is to add the feed to the layout.

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

      Go to app/views/layouts/application.html.erb and add this your <head></head> section:

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

      <小时>

      其中可能有一两个错字,所以如果这对您有用,请告诉我.


      There may be a typo or two in that, so let me know if this works for you.

      这篇关于在 Rails 3 中生成 RSS 提要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆