如何将rss feed链接到url

时间:2013-07-24 16:44:36

标签: ruby rss sinatra

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

1 个答案:

答案 0 :(得分:0)

http://recipes.sinatrarb.com/p/views/rss_feed_with_builder

无耻地偷走了

安装或添加Builder gem:

# 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

进一步阅读: