Method A:
Is that you put together the format of the RSS XML, output. and set the HTTP Header, Mark Content-type as Application/xml, common code:
Copy Code code as follows:
#Post_controller:: Feed ()
def feed
Require "RSS"
Articles = Article.find:all,: Order => ' Post_date DESC ',: Limit => 10
feed = Rss::maker.make ("2.0") do |maker|
Maker.channel.title = "Gang of Technology"
Maker.channel.description = "Gang of Technology site"
Maker.channel.link = "Http://up-u.com"
Maker.items.do_sort = True
Articles.each do |article|
item = Maker.items.new_item
Item.link = "Http://www.***.com/archives/#{article.id}"
Item.title = Article.title
Item.date = Article.post_date
Item.description = ""
End
End
Send_data feed.to_s,: Type => "Application/rss+xml",:d isposition =>
' Inline '
End
Method B:
Rails controller->action Code:
Copy Code code as follows:
#Post_controller:: Feed
def feed
@posts =post.find:all,:limit=> "id desc"
End
Erb Template:
xml.instruct!
Xml.rss ("Version" => "2.0″,
"XMLNS:DC" => "HTTP://PURL.ORG/DC/ELEMENTS/1.1/") do
Xml.channel do
Xml.title "Renlu.xu ' s Blog"
Xml.link (Url_for (:action=> "start": Only_path=>false))
Xml.description "My Life, My Love"
Xml.language "ZH_CN"
Xml.ttl 60
For event in @posts do
Xml.item do
Xml.title (Event.title)
Xml.description (Event.body)
Xml.pubdate (event.created_at.to_s (: rfc822))
Xml.guid (event.id)
Xml.link ("Http://.....#{event.id}")
End
End
End
End
This method on the Internet many articles to this step is over, there is no below, in fact, wrong. To this step, access to Http://localhost:3000/post/feed is still coming out of the HTML interface. G, find an Introduction: Rails 2.0 renders the template in different formats. This code is not used in/views/post/feed.rhtml, it needs to be placed in/views/post/feed.atom.builder, and it needs to go through http://localhost:3000/post/feed/ 123.atom (here 123 has no real meaning to scratch a random), or http://localhost:3000/post/feed?format=atom to correctly press Rss+xml output.