gpt4 book ai didi

ruby-on-rails - 使用 Rails 创建 RSS 提要

转载 作者:行者123 更新时间:2023-12-04 03:41:53 25 4
gpt4 key购买 nike

我正在尝试为我的新闻帖子创建一个 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" 但是当我转到该页面时没有任何内容呈现..

最佳答案

这是我最终得到的代码:

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

关于ruby-on-rails - 使用 Rails 创建 RSS 提要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17742543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com