Create an RSS feed in Rails Tian

本文指导如何在Rails应用中整合RSS订阅功能,并通过简单的代码实现浏览器自动检测RSS feed,增强用户粘性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Not too long ago, I showed you how to create a blog archive for your Rails blog. Well, creating an RSS feed for your Rails blog might not necessarily increase SEO like a Google Sitemap can, but it's a helluva great way to stay in touch with your readers.

Here's a walk through for integrating RSS with your Rails blog.

RSS and Ruby on Rails - The Full Story

app/controllers/posts_controller.rb
@posts = Post.all(:select => "title, author, id, content, posted_at", :order => "posted_at DESC", :limit => 20) 

respond_to do |format|
   format.rss { render :layout => false }
end

In Rails, this call sends all RSS calls to index.rss.builder by default. Let's take a look at what that View does.

app/views/posts/index.rss.builder

This is where all the Railsy magic happens. In another language, in another web application framework, this could've easily been ugly. But with Rails, we're talking rainbows and ponies baby!

xml.instruct! :xml, :version => "1.0" 
xml.rss :version => "2.0" do
  xml.channel do
    xml.title "Your Blog Title"
    xml.description "A blog about software and chocolate"
    xml.link posts_url

    for post in @posts
      xml.item do
        xml.title post.title
        xml.description post.content
        xml.pubDate post.posted_at.to_s(:rfc822)
        xml.link post_url(post)
        xml.guid post_url(post)
      end
    end
  end
end

You just implemented the RSS specification in Ruby on Rails. Congrats! Let's keep moving, we're almost done.

app/views/layouts/application.html.erb

You can get browsers to auto-detect your Rails blog rss feed with a single line of Ruby on Rails code:

<%= auto_discovery_link_tag(:rss, "http://iblog.com") %>

Or, if you want, you can use straight XHTML to get browsers to auto-detect your Rails blog rss feed:

<link href="http://iblog.com" rel="alternate" title="RSS" type="application/rss+xml" />


and


http://www.wuwx.net/archives/25 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值