Rails makes RSS feed code _ajax related

Source: Internet
Author: User
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.

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.