我正在尋找用於在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