原创作者: hideto   阅读:872次   评论:0条   更新时间:2011-05-26    
acts_as_taggable/brower_filters/resource_feeder/simply_helpful这四个插件放在legacy包下面,因为它们已经停止更新和维护
我们安装时可以用
ruby script/plugin install legacy

这将同时安装这四个插件
或者使用
ruby script/plugin install http://dev.rubyonrails.org/svn/rails/plugins/legacy/resource_feeder/

今天我们就来看Rails的RSS和ATOM插件resource_feeder
该插件依赖于simply_helpful
这个插件的使用就非常简单了
class BooksController < ApplicationController

  def rss
    render_rss_feed_for Book.find(:all, \:order => 'created_at DESC', :limit => 10), {
    	:feed => {
	    	:title => "Recent Book",
  	  	:pub_date => :created_at
  	  }
    }
  end

end

我们来看看核心代码rss.rb:
require 'resource_feeder/common'

module ResourceFeeder
  module Rss
    include ResourceFeeder::Common
    extend self

    def render_rss_feed_for(resources, options = {})
      render :text => rss_feed_for(resources, options), :content_type => Mime::RSS
    end

    def rss_feed_for(resources, options = {})
      xml = Builder::XmlMarkup.new(:indent => 2)

      options[:feed]       ||= {}
      options[:item]       ||= {}
      options[:url_writer] ||= self

      if options[:class] || resources.first
        klass      = options[:class] || resources.first.class
        new_record = klass.new
      else
        options[:feed] = { :title => "Empty", :link => "http://example.com" }
      end
      use_content_encoded = options[:item].has_key?(:content_encoded)

      options[:feed][:title]    ||= klass.name.pluralize
      options[:feed][:link]     ||= SimplyHelpful::PolymorphicRoutes.polymorphic_url(new_record, options[:url_writer])
      options[:feed][:language] ||= "en-us"
      options[:feed][:ttl]      ||= "40"

      options[:item][:title]           ||= [ :title, :subject, :headline, :name ]
      options[:item][:description]     ||= [ :description, :body, :content ]
      options[:item][:pub_date]        ||= [ :updated_at, :updated_on, :created_at, :created_on ]

      resource_link = lambda { |r| SimplyHelpful::PolymorphicRoutes.polymorphic_url(r, options[:url_writer]) }

      rss_root_attributes = { :version => 2.0 }
      rss_root_attributes.merge!("xmlns:content" => "http://purl.org/rss/1.0/modules/content/") if use_content_encoded

      xml.instruct!

      xml.rss(rss_root_attributes) do
        xml.channel do
          xml.title(options[:feed][:title])
          xml.link(options[:feed][:link])
          xml.description(options[:feed][:description]) if options[:feed][:description]
          xml.language(options[:feed][:language])
          xml.ttl(options[:feed][:ttl])

          for resource in resources
            xml.item do
              xml.title(call_or_read(options[:item][:title], resource))
              xml.description(call_or_read(options[:item][:description], resource))
              if use_content_encoded then
                xml.content(:encoded) { xml.cdata!(call_or_read(options[:item][:content_encoded], resource)) }
              end
              xml.pubDate(call_or_read(options[:item][:pub_date], resource).to_s(:rfc822))
              xml.guid(call_or_read(options[:item][:guid] || options[:item][:link] || resource_link, resource))
              xml.link(call_or_read(options[:item][:link] || options[:item][:guid] || resource_link, resource))
            end
          end
        end
      end
    end
  end
end

其中xml.pubDate(call_or_read(options[:item][:pub_date], resource).to_s(:rfc822))这行代码运行时说to_s方法报错"wrong number of arguments(1 for 0)"
我把该行代码的to_s方法的参数:rfc822去掉即可成功运行
评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

Global site tag (gtag.js) - Google Analytics