2

我正在尝试为我的新闻帖子创建一个 RSS 提要,我用谷歌搜索它并想出了以下代码:

def feed
  @posts = News.all(:conditions => "#{Settings.show} = 1", :select => "id, title, heading, content, date_posted", :order => "date_posted DESC") 

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

然后在一个名为“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.date_posted.to_s(:rfc822)
        xml.link post_url(post)
        xml.guid post_url(post)
       end
    end
  end
end

我已将它添加到我的路由文件match "/news/feed" => "aboutus#feed"中,但是当我转到该页面时,什么都没有呈现..

4

2 回答 2

5

这是我最终得到的代码:

def news

@news = News.find(:all, :order => "date_posted desc", :conditions => "#{Settings.show} = 1")
render :template => 'about-us/news/feed.rss.builder', :layout => false

end

和:

xml.instruct! :xml, :version => "1.0" 
xml.rss :version => "2.0" do
  xml.channel do
    xml.title "News"
    xml.description "Description"
    xml.link "/news"

for post in @news
  xml.item do
    xml.title post.title
    xml.description post.content
    xml.pubDate post.date_posted.to_s(:rfc822)
    xml.link "/news/#{post.reference}"
    xml.guid "/news/#{post.reference}"
    xml.icon "/favicon.ico"
   end
end
end
end
于 2013-07-22T13:33:26.347 回答
1

是否有可能它工作正常,但您的浏览器没有显示 RSS?

您可以尝试curl在终端中使用以查看是否正在呈现任何内容:

curl http://localhost:3000/news/feed
于 2013-07-19T14:44:36.263 回答