添加route: 项目下=》config目录=》route.rb目录;
新建feed_controller.rb controller
class FeedController < ApplicationController
layout 'xx_application'
require "rss/2.0"
require 'rss/maker'
def index
params[:catalog] = "reference"
@category = Category.find_by!(catalog: params[:catalog])
if @category.is_root?
@navs = @category.children.valid.sort_order
@main_category = @category
else
@navs = @category.siblings.valid.sort_order
@main_category = @category.parent
end
category_ids = [@category.id] + @category.child_ids
@articles_categories = ArticlesCategory.includes(:author, :article).where(category_id: category_ids).published.sort_order.page(params[:page] || 1).per_page(20)
respond_to do |format|
format.rss { render :layout => false }
end
# render 'index'
# feed = RSS::Maker.make("2.0") do |maker|
# maker.channel.title = @title
# maker.channel.description = @description
# maker.channel.link = "https://www.xx.com/reference"
# maker.channel.author = "xxx"
# maker.channel.updated = Time.now.to_s
# maker.channel.about = @linkUrl
# maker.channel.language = 'sdf'
#
# maker.items.do_sort = true
# @articles_categories.each do |article|
# item = maker.items.new_item
# #item.link = "https://www.xx.com/news/#{currentDate}#{article.id}"
# item.link = "https://www.x.cxom/news/#{@currentDate}#{article.id}"
# item.title = article.title
# item.date = article.published_at
# #item.description = Maruku.new(article.content).to_html
# end
#end
# send_data feed.to_s, :type => "application/rss+xml", :disposition => 'inline'
end
end
在app\views\feed下新建文件 index.rss.builder
xml.instruct! :xml, :version => "1.0"
xml.rss :version => "2.0",
:'xmlns:atom' => "http://www.w3.org/2005/Atom",
:'xmlns:content' => "http://purl.org/rss/1.0/modules/content/",
:'xmlns:wfw' => "http://wellformedweb.org/CommentAPI/",
:'xmlns:sy' => "http://purl.org/rss/1.0/modules/syndication/",
:'xmlns:slash' => "http://purl.org/rss/1.0/modules/slash/" do
xml.channel do
xml.title "xx财经"
xml.description "提供财经数据等栏目及产品。"
xml.link "https://www.xx.com/feed"
xml.language "ZH-CN"
# xml.atom :link do
# xml.rel 'hub'
# xml.href 'http://pubsubhubbub.appspot.com'
# end
xml.atom :link,
'rel' => 'hub',
'href' => 'http://pubsubhubbub.appspot.com'
#xml.atom :link(:'rel'=>'hub' , :'href'=>'http://pubsubhubbub.appspot.com', 'type'=>"application/rss+xml")
for @article in @articles_categories
xml.item do
publishtDate = Date.parse(@article.published_at.to_s).strftime("%Y%m%d")
id = @article.id
articleUrl = "http://www.xx.com/news/#{publishtDate}/#{id}"
xml.link articleUrl
xml.title @article.title
xml.guid articleUrl
xml.description do
xml.cdata! @article.summary
end
xml.content :encoded do
xml.cdata!(@article.content)
end
xml.pubDate @article.published_at.to_s(:rfc822)
end
end
end
end
校验rss是否有效:
http://validator.w3.org/feed/ 输入路径:xxx/feed
最后去红板报 媒体查看。