shitou's blog 
Home About Feed | MIX BOY 塌客

Tags "xml"

23
Jul

用rails生成RSS by shitou

0

在rails中生成RSS,不需要使用其他插件,rails自动调用的ruby已内置XML框架,直接用就行了


Making RSS feeds for your Rails applications is one of those things that isn’t documented in very many places, but turns out to be fairly easy to do. Adding RSS feeds to your site is one of the best things you can do to retain a steady visitor base. It gives your visitors an easy way to be notified when you update your site with new content. Here’s the three parts you need to code in order to add an RSS feed.

    

The Controller Action

    

In the controller for your site you need to add an action for the feed generation, let’s call ours “feed”.

  
def feed
  @posts = Post.find(:all, :limit => 10, :order => "created_on DESC")
end
    

This will get the most recent ten posts out of our database and make them available for us to use in the builder template.

    

Also, if you are using a layout for your controller you need to not have it render for your feed or else it won’t work correctly. Something like this will fix it:

  
layout "my_layout", :except => [:feed]
    

The Builder Template

    

Builder templates are different from RHTML templates in that they represent a structured XML template instead of an HTML template.  They also end in .rxml instead of .rhtml.  Here’s the template we want to use that will follow the RSS 2.0 specification.  Here’s what our feed.rxml will look like.

  
xml.instruct! :xml, :version=>"1.0" 
xml.rss(:version=>"2.0"){
  xml.channel{
    xml.title("Your Blog's Title")
    xml.link("http://www.yoursite.tld/")
    xml.description("What your site is all about.")
    xml.language('en-us')
      for post in @posts
        xml.item do
          xml.title(post.title)
          xml.description(post.html_content)      
          xml.author("Your Name Here")              
          xml.pubDate(post.created_on.strftime("%a, %d %b %Y %H:%M:%S %z"))
          xml.link(post.link)
          xml.guid(post.link)
        end
      end
  }
}
    

If you compare this against the RSS 2.0 Specification you’ll see how it mirrors an RSS 2.0 XML file. The important points to note are that the strftime conversion for the pubDate is necessary, and that I’m using the link for each post as the guid as well since it is fairly safe to assume each post will have it’s own unique link. Your Post object may look slightly different, so make sure you make appropriate changes as necessary to this template. I also make the post description the actual content of the post since I like full-text feeds.

    

The Auto-Discovery Tag

    

The last bit to finish off your brand new spankin’ RSS feed is the auto-discovery tag.

  
<%= auto_discovery_link_tag(:rss, :action => 'feed') %>
    

Place it within the element of your site’s layout (if you use one), that way your visitors using a browser such as Safari or Firefox will be able to easily see that your site has an RSS feed.

    

Final Notes

    

So that’s it for generating an RSS feed in Rails. Fairly easy, huh? The only other thing to really note is that you are free to define how you want to get objects out of your database to add to the feed. Posts only relating to food, just pictures, or whatever. Just expand that :conditions symbol for the find method as needed.

Tags: rails,rss,xml

2008-07-23 20:15:24, 852 reviews

send to mailbox

Your email:

Tags

U-ka saegusa IN db command Mai Kuraki Norah Jones log iPhoneException ACG Mac Safari objective-c CouchDB LVS AJAX debian 推荐 AMQP google mail bug gettext Erlang 北京 iptables 架构 tips mysql backup function 我看 postfix 监控 SEO cache Etag memcache thread 进程 线程 无锡 yield file column mixboy xml rss gems ruby shitou shell lighttpd 安全 csrf 公司 nginx linux 模块 apache webserver 朋友 大学 生活 尼古拉斯凯奇 movie 文件同步 笑笑 歌词 auto complete plugin rails music ubuntu blog

Category

  • iPhone[17]
  • Erlang[4]
  • google[8]
  • 生活[38]
  • 音乐[11]
  • 电影[11]
  • linux[20]
  • web server[6]
  • mail server[3]
  • cluster[1]
  • system manage[5]
  • ruby[18]
  • ruby on rails[27]
  • 开源[3]

Episode

  • iPhoneException
  • shell
  • thread
  • memcache

Recent Comments

  • comment6, viagra bez receptu, revelation, ambie...
  • comment5, http://www.freecodesource.com/user/pr...
  • comment4, meridia medicine, :-<>], plavix clopi...
  • comment2, viagra calgary, resin, paxil vs wellb...
  • comment2, codeine promethazine, conclusion, buy...
  • comment2, clomid ovarian cysts, fronted, oxycod...
  • comment2, http://www.freecodesource.com/user/pr...
  • comment3, cipro allergy, :-E, weed and hydrocod...
  • comment2, http://www.freecodesource.com/user/pr...
  • comment4, vicodin cr, chocolate, premarin cance...

Popular Posts

  • MySQL Innodb备份
  • 准备开始学习Erlang了(恶狼, 二郎..)
  • Lighttpd配置参数
  • iPhone上的HelloWorld终于跑起来了
  • Etag和Expire

Recommended Posts

  • Mai Kuraki -永远より ながく
  • U-ka saegusa IN db Final Best
  • Heaven Can Wait - Charlotte Gainsbou
  • Ruby遍历MemCached的key
  • Norah Jones - The fall
  • 请记得仰望梦想的姿势
  • Shell: 统计MySQL InnoDB表的大小
  • Rails Benchmark
  • 发送异常到邮箱
  • I Miss Nobody
  • Music4u, Vol. 1
  • my macbook
  • Mai Kuraki-Beautiful
  • 10首最伤情英文歌曲精选
  • Mai Kuraki - PUZZLE/Revive

Friends' blogs

  • levy
  • sphance
  • andreas

Login

   注册

留言 查看留言

留言

   取消

留言 查看留言


Statistics

  • 访问次数: 51144
  • 今天访问: 8
  • 日志: 172
  • 评论: 124
  • 音乐: 9
  • 用户: 148


 

just DO NOT support IE

close