0

我正在使用 Ruby 的 RSS 类在 Sinatra 应用程序中生成 RSS 提要。每个用户的提要都是唯一的。如何将提要连接到 URL,以便用户可以使用他们的 RSS 阅读器进行订阅,并自动接收新的提要更新?

4

1 回答 1

0

无耻地从http://recipes.sinatrarb.com/p/views/rss_feed_with_builder窃取

安装或添加Buildergem:

# add to Gemfile and run `bundle`...
gem 'builder'

# ... or install system-wide
$ gem install builder  

将适当的路由添加到您的应用程序/控制器:

# in app.rb
get '/rss' do
  @posts = # ... find posts
  builder :rss
end

创建视图以显示 RSS 提要:

# in views/rss.builder
xml.instruct! :xml, :version => '1.0'
xml.rss :version => "2.0" do
  xml.channel do
    xml.title "Cool Website News"
    xml.description "The latest news from the coolest website on the net!"
    xml.link "http://my-cool-website-dot.com"

    @posts.each do |post|
      xml.item do
        xml.title post.title
        xml.link "http://my-cool-website-dot.com/posts/#{post.id}"
        xml.description post.body
        xml.pubDate Time.parse(post.created_at.to_s).rfc822()
        xml.guid "http://my-cool-website-dot.com/posts/#{post.id}"
      end
    end
  end
end

进一步阅读:

于 2013-07-24T20:52:21.610 回答