ruby rss源制作

添加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

在这里插入图片描述

在这里插入图片描述
最后去红板报 媒体查看。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值