Method:
You just put together the rss xml format, output it, set the HTTP Header, and Mark content-type as application/XML. Common Code:
Copy codeThe Code is 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",: disposition =>
'Line'
End
Method B:
Rails Controller-> Action code:
Copy codeThe Code is 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 is not suitable for many articles on the Internet. In this step, access http: // localhost: 3000/post/feed is still the html interface. G. Find the introduction: rails 2.0 renders the template according to different formats. Put this code in/views/post/feed. rhtml is useless and needs to be placed in/views/post/feed. atom. builder, and you need to pass http: // localhost: 3000/post/feed/123. atom (here 123 does not actually mean anything to catch), or http: // localhost: 3000/post/feed? Format = atom can be correctly output by rss + xml.