Car*_*ssi 2 rss haml controller ruby-on-rails feed
我正在尝试为我的新闻帖子创建一个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
Run Code Online (Sandbox Code Playgroud)
然后在一个名为"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
Run Code Online (Sandbox Code Playgroud)
我已将它添加到我的路线文件中,match "/news/feed" => "aboutus#feed"但是当我转到该页面时,没有任何内容呈现.
这是我最终得到的代码:
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
Run Code Online (Sandbox Code Playgroud)
和:
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
Run Code Online (Sandbox Code Playgroud)