Ruby on rails 3 在Rails 3中生成RSS提要

Ruby on rails 3 在Rails 3中生成RSS提要,ruby-on-rails-3,rss,Ruby On Rails 3,Rss,我正在寻找在Rails3中生成提要的最佳实践/标准模式。仍然有效吗?首先,现在我建议使用ATOM提要而不是RSS ATOM提要的规范在国际化、内容类型和其他方面提供了比RSS提要更大的价值,每个现代提要阅读器都支持它 有关ATOM vs RSS的更多信息,请访问: 以及关于该主题的博客帖子 关于编码: 本例假设: 名为NewsItem的模型,具有以下属性: 标题 内容 author\u name 该模型的控制器(news\u items\u controller.rb),您将向其添

我正在寻找在Rails3中生成提要的最佳实践/标准模式。仍然有效吗?

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

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

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

  • 以及关于该主题的博客帖子

关于编码:

本例假设:

  • 名为
    NewsItem
    的模型,具有以下属性:
    • 标题
    • 内容
    • author\u name
  • 该模型的控制器(
    news\u items\u controller.rb
    ),您将向其添加
    提要
    操作
我们将为此使用一个构建器模板,RubyonRails非常有用

1。将操作添加到控制器

转到
app/controllers/news\u items\u 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
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
resources :news_items
match '/feed' => 'news_items#feed',
      :as => :feed,
      :defaults => { :format => 'atom' }
2。设置生成器模板

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

转到
app/views/news\u items/feed.atom.builder
并添加:

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
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
resources :news_items
match '/feed' => 'news_items#feed',
      :as => :feed,
      :defaults => { :format => 'atom' }
3。用线路将其连接起来

让我们在

默认情况下,这将使用ATOM格式调用操作,并将
/feed.rss
重定向到
/feed.ATOM

转到
config/routes.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
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
resources :news_items
match '/feed' => 'news_items#feed',
      :as => :feed,
      :defaults => { :format => 'atom' }
4。在版面上添加指向ATOM和RSS源的链接

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

转到
app/views/layouts/application.html.erb
并将此添加到您的
部分:

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



这里面可能有一两个输入错误,所以请告诉我这是否适合您。

我做了类似的操作,但没有创建新的操作

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

控制器
值得称赞的是,这是我根据这篇博文及其评论所做和使用的一个实现:@holden-因为我喜欢代码重用,并将其作为几个项目的一个模块使用。这样,如果我想更改某些内容,这是一次性的更改,不会影响索引操作逻辑(例如,像您在回答中所做的那样禁用提要格式的分页)。“个人偏好,两者同样有效。”托梅杜阿尔特:最佳答案ever@tomeduarte回答很好,但我认为我说得对,根据你建议的路线,你提供的自动发现链接标签是不正确的。它们实际上应该是“/feed”和“/feed.rss”。我知道你说过可能有打字错误,所以我确信这只是一个疏忽。:-)如果我想为文章显示一个图像,我该如何传递图像条目呢?你的例子看起来很简单——这只是让atom提要正常工作的两个步骤还是更多的步骤?@ubique:出于好奇,我只是在我的一个控制器中玩了这个。它使用了新的Rail 3格式,所以我所要做的就是将:atom添加到我的respond\u到hash中——我甚至不必触摸respond\u。Rails自动找到index.atom.builder,对控制器没有进一步修改。我对来自PHP的背景印象深刻!如果我想为文章显示一个图像怎么传递图像条目。