使用Rails创建RSS源

时间:2013-07-19 09:24:56

标签: ruby-on-rails controller rss haml feed

我正在尝试为我的新闻帖子创建一个RSS提要,我用Google搜索并提出了这段代码:

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"中,但当我转到该页面时,没有任何内容呈现..

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

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

curl http://localhost:3000/news/feed