Create an rss feed code using rails

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.