shitou's blog 

  • Home
  • About
  • Feed
  • 十人族
  • MIX BOY
  • 塌客

Category "ruby on rails"

23
Jul

用rails生成RSS

在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, 1330 reviews, comment

send to mailbox

Your email:

Related Posts

插件auto_complete的使用

【转载】CSRF攻击:不容忽视

MIX BOY--自己写的音乐搜索程序

file_column上传中文文件名的问题

ruby解析RSS

TOP

Tags

json Impactjs Canvas fun gen_server superfly PS3 Webgame HTML5 jquery SSH tenerer MooseFS gearman-ruby Gearman MongoDB MochiChat TCP 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

  • HTML5[3]
  • iPhone[17]
  • Erlang[11]
  • google[13]
  • 生活[45]
  • 音乐[13]
  • 电影[11]
  • linux[29]
  • web server[6]
  • mail server[3]
  • cluster[1]
  • system manage[9]
  • ruby[20]
  • ruby on rails[28]
  • 开源[4]

Episode

  • MongoDB
  • MochiChat
  • iPhoneException
  • shell
  • thread
  • memcache


Popular Posts

  • 准备开始学习Erlang了(恶狼, 二郎..)
  • Ruby遍历MemCached的key
  • 服务器监控小脚本
  • MySQL Innodb备份
  • [转载]5个有用的ruby gems

Recommended Posts

  • Tri-survive - HTML5 Game
  • Cut the rope - HTML5版
  • json_formatter
  • 在gen_server中spawn新的进程
  • Superfly - Wildflowers
  • 继续凸墙 for Mac OS
  • MongoDB Beijing 2011
  • MongoDB删除map_reduce生成的tmp collection
  • Mai Kuraki - Future Kiss
  • Erlang OOP
  • 用SSH tunnel凸墙
  • Google的语法高亮工具包
  • 大量数据的批量操作
  • 再次被和谐-_-
  • 十人族: 上线了

Friends' blogs

  • levy
  • sphance
  • andreas
  • yangkunlun
  • {:dev=>:wxianfeng}
  • bheye
  • joeydarko

Login

   注册

留言 查看留言

留言

   取消

留言 查看留言


Statistics

  • 访问次数: 316242
  • 今天访问: 37
  • 日志: 213
  • 评论: 4967
  • 音乐: 9
  • 用户: 1758


少女,不点下广告吗!

 

all by shitou

blog comments powered by Disqus

close