Ruby-on-rails 如何在 Rails 3 中制作 RSS/Atom 提要?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3951235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:24:12  来源:igfitidea点击:

How do I make an RSS/Atom feed in Rails 3?

ruby-on-railsrssruby-on-rails-3

提问by simonista

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?

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

Thanks!

谢谢!

回答by Matt Lennard

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:

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

controllers/posts_controller.rb

控制器/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

意见/帖子/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

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

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

回答by thatmiddleway

Using the auto_discovery_link_tag:

使用 auto_discovery_link_tag:

In the controller:

在控制器中:

respond_to do |format|
  format.html
  format.atom {render action: 'index', layout: false}
end