如何在 Rails 3 中制作 RSS/Atom 提要? [英] How do I make an RSS/Atom feed in Rails 3?

查看:56
本文介绍了如何在 Rails 3 中制作 RSS/Atom 提要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Rails 3 的新手,我正在尝试制作 RSS/Atom 提要.我知道 auto_discovery_link_tag,但是什么是关联的控制器/动作应该是什么样子?

I'm pretty new to Rails 3, and I'm trying to make an RSS/Atom feed. I know about auto_discovery_link_tag, but what is the associated controller/action supposed to look like?

谢谢!

推荐答案

Auto_discovery_link_tag 是一个好的开始.快速谷歌搜索,我找到了关于如何创建的博客文章Rails 中的 RSS 提要.让我告诉您关联的控制器/操作应该是什么样子:

Auto_discovery_link_tag is a good start. A quick Google search and I found blog posts on How to Create an RSS feed in Rails. Let me fill you in on what your associated controller/action is supposed to look like:

controllers/posts_controller.rb

controllers/posts_controller.rb

def feed
    @posts = Post.all(:select => "title, author, id, content, posted_at", :order => "posted_at DESC", :limit => 20) 

    respond_to do |format|
      format.html
      format.rss { render :layout => false } #index.rss.builder
    end
end

此文件的名称应与控制器匹配.见下文:

The name of this file should match the controller. See, below:

views/posts/feed.rss.builder

views/posts/feed.rss.builder

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

这是所有 Railsy 魔法发生的地方.在这里,RSS 提要 XML 被生成并返回到 HTTP.

This is where all the Railsy magic happens. Here, the RSS feed XML is generated and returned to HTTP.

这篇关于如何在 Rails 3 中制作 RSS/Atom 提要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆