`
hideto
  • 浏览: 2683610 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

每天一剂Rails良药之Syndicate Your Site With RSS

    博客分类:
  • Ruby
阅读更多
今天来看看Rails对RSS的支持:
feed_controller.rb:
class FeedController < ApplicationController
  session \:off

  def recipes
    @recipes = Recipe.find(:all,
                           \:order => "updated_at, created_at",
                           :limit => 15)
    @headers["Content-Type"] = "application/rss+xml"
  end
end

因为RSS请求是无状态的,所以session \:off可以提高性能
recipes.rxml:
xml.instruct!
xml.rss "version" => "2.0"
        "xmlns:dc" => "http://purl.org/dc/elements/1.1/" do
  xml.channel do
    xml.title 'Recipes on Rails'
    xml.link url_for(\:only_path => false,
                     :controller => 'recipes',
                     :action => 'list')
    xml.pubDate CGI.rfc1123_date(@recipes.first.updated_at)
    xml.description h("Recipes created for and by guys who shouldn't be cooking.")
    @recipes.each do |recipe|
      xml.item do
        xml.title recipe.title
        xml.link url_for(\:only_path => false,
                         :controller => 'recipes',
                         :id => recipe)

        xml.description h(recipe.instructions.to_s)
        xml.pubDate CGI.rfc1123_date(recipe.updated_at)
        xml.guid url_for(\:only_path => false,
                         :controller => 'recipes',
                         :action => 'show',
                         :id => recipe)
        xml.author h(recipe.author.name)
      end
    end
  end
end

这样生成的RSS feed的例子为:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Recipes on Rails</title>
    <link>http://myserver:2007/recipes/list</link>
    <pubDate>Fri, 03 Mar 2007 04:53:50 GMT</pubDate>
    <description>
      Recipes created for and by guys who shouldn't be cooking.
    </description>
    <item>
      <title>Canned Fish and Chips</title>
      <link>http://myserver:2007/recipes/show/6</link>
      <description>
        1. Open can. 2. Empty contents into bowl. 3. Serve.
      </description>
      <pubDate>Fri, 03 Mar 2007 04:58:42 GMT</pubDate>
      <guid>http://:2003/recipes/show/6</guid>
      <author>David</author>
    </item>
  </channel>
</rss>

另外我们可以在网站首页的<head>标签里加上以下内容以便于RSS订阅工具自动找到本网站的RSS:
<%= auto_discovery_link_tag(:rss,
                            {:controller => 'feed', :action => 'recipes'}) %>

这段代码将为我们生成<link>标签,如:
<link href="http://www.chadfowler.com/index.cgi?rss"
      rel="alternate"
      title="RSS"
      type="application/rss+xml" />


BTW:蛙眼把文章中的“: o”用笑脸图片来替换,真是很弱智好不好。
分享到:
评论
2 楼 hideto 2008-07-22  
1 楼 ShiningRay 2008-07-21  
pubdate最好是用rfc 822的格式

用Time.rfc2822

相关推荐

    Agile+Web+Development+with+Rails中文版.pdf

    《Agile Web Development with Rails》是一本经典的Rails开发指南,中文版的出版使得更多的中国开发者能够深入理解并应用敏捷开发方法与Ruby on Rails框架。这本书是Rails开发者的必备参考资料,它详细介绍了如何...

    Agile Web Development with Rails 4

    Ruby on Rails helps you produce high-quality, beautiful-looking web applications quickly. You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of ...

    Agile Web Development with Rails

    《Agile Web Development with Rails》(敏捷Web开发:Ruby on Rails)这本书,作为Rails开发新手的教材,强调了敏捷开发方法,并以其帮助开发者建立起一个实用的Web应用。从给出的文件信息来看,这本书正在编写过程...

    Agile Web Development With Ruby On Rails第一及第二版

    《Agile Web Development With Ruby On Rails》是两本广受欢迎的书籍,主要涵盖了使用Ruby on Rails框架进行敏捷Web开发的知识。这本书的第1版和第2版分别详细讲解了如何运用敏捷开发方法来构建高效、可扩展且易于...

    Agile Web Development with Rails (PDF)

    Rails作为一款基于Ruby语言的Web应用框架,自诞生之初就秉持着敏捷开发的理念。它强调代码的简洁性、易读性和快速迭代能力,这些特性与敏捷开发的原则不谋而合。通过Rails,开发者可以迅速构建功能丰富的Web应用,...

    [Michael Hartl] Ruby on Rails Tutorial

    Learn Web Development with Rails Clear EPUB version in English, Second Edition “The author is clearly an expert at the Ruby language and the Rails framework, but more than that, he is a working ...

    Rails上的API:使用Rails构建REST APIAPIs on Rails: Building REST APIs with Rails

    Rails以其简洁优雅的语法、高效的开发速度以及良好的社区支持而闻名,这使得它成为构建现代API的理想选择之一。 ### 一、什么是RESTful API REST(Representational State Transfer)是一种软件架构风格,用于定义...

    Agile Web Development with Rails 3nd Edition Beta.pdf

    通过上述分析,我们可以看到,《敏捷Web开发与Rails》第三版不仅是一本关于Rails框架的技术书籍,更是一部指导开发者如何运用敏捷方法论进行Web开发的经典之作。无论对于初学者还是经验丰富的开发者而言,本书都具有...

    Rails Recipes英文版(清晰文字pdf+源码)

    Ruby三神书之一(其余的两本是Agile.Web.Development.with.Rails和Ruby For Rails,在我的资源列表也有) Rails is large, powerful, and new. How do you use it effectively? How do you harness the power? And, ...

    Ruby on Rails教程:学习使用Rails进行Web开发Ruby on Rails Tutorial: Learn Web Development with Rails

    本书教您如何使用Ruby on Rails开发和部署真正的,具有工业实力的Web应用程序,Ruby on Rails是为诸如Twitter,Hulu,GitHub和Yellow Pages等顶级网站提供支持的开源Web框架。

    Ruby on Rails Tutorial - Learn Web Development with Rails 3rd

    本书《Ruby on Rails 教程 —— 使用 Rails 学习 Web 开发》第三版是由 Michael Hartl 编写的一本全面介绍 Ruby on Rails 的教程。该书面向希望学习如何使用 Ruby on Rails 进行 Web 应用开发的初学者和中级开发者。...

    Agile Web Development with Rails 4th edition(敏捷Web开发与Rails:程序指南 第四版)

    1. **ActiveRecord**: ActiveRecord是Rails的核心组件之一,负责处理数据库交互。在第四版中,对ActiveRecord的查询接口进行了优化,提供了更丰富的查询方法,如`pluck`, `exists?`, 和 `includes`,这些都极大地...

    Rails.Angular.Postgres.and.Bootstrap.2nd.Edition

    With this fully revised new edition, take a holistic view of full-stack development to create usable, high-performing applications with Rails 5. Rails is a great tool for building web applications, ...

Global site tag (gtag.js) - Google Analytics