我正在寻找用于在Rails 3中生成提要的最佳实践/标准模式。是否http://railscasts.com/episodes/87-generating-rss-feeds仍然有效?在Rails 3中生成RSS提要
50
A
回答
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" %>
可能有一个错字或两个在,所以让我知道这对你的作品。
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
相关问题
- 1. Django使用描述生成RSS提要
- 2. RSS提要的生成和实现
- 3. 如何在Rails 3.x视图中呈现RSS提要?
- 4. 在ASP.NET 3.5中生成媒体RSS(MRSS)提要
- 5. 如何生成RSS提要并提供给用户
- 6. 什么是解析RSS提要的好的Rails 3 gem?
- 7. Rails 3帮助构建简单的RSS提要
- 8. 如何覆盖DSpace中RSS提要的生成?
- 9. 从链接列表中即时生成RSS提要?
- 10. ColdFusion生成的rss提要中的格式描述
- 11. 在RSS提要PHP
- 12. 缺少脚本/在Rails 3中生成
- 13. 在rails 3中自动生成路径?
- 14. 在Rails 3中生成哈希表格
- 15. RSS提要
- 16. Youtube RSS提要
- 17. 从RSS提要
- 18. 从Ruby-on-Rails生成RSS订阅源
- 19. 快速生成/发布来自ASP.NET项目的RSS提要?
- 20. 用XML制作的SyndicationFeedFormatter生成定制的RSS/Atom提要
- 21. 如何生成合并的RSS提要(来自DokuWiki和Serendipity)
- 22. 为Twitter rss提要生成缓存文件
- 23. 什么是好的scala库来生成RSS提要?
- 24. 如何在rails中创建密码保护的RSS提要
- 25. 在Rails中创建RSS提要时无效无对象异常
- 26. 如何在Rails 4.0中缓存解析的RSS提要?
- 27. 在Asp.Net中生成RSS MVC 3返回纯文本XML
- 28. excel中的RSS提要
- 29. 自动将RSS提要提交至RSS提要目录
- 30. 解析RSS提要
要给予信贷,这是因为,这是我做的实施和使用基于关闭这个博客帖子和评论意见:http://lindsaar.net/2010/2/12/how -to-使-AN-RSS-饲料或原子饲料,在护栏 – tomeduarte 2011-01-28 20:14:28
但为什么添加了一个全新的行动,而不是简单一个新的格式表示索引操作? – holden 2011-02-05 08:57:17