Get the App
SLTechnology News&Howtos  ›  Development  › 

Rails makes the code sharing of rss feed

Shulou Source: shulou.com Published: 2022-06-03 19:35:51 09月30日 Update

This article introduces the knowledge of "rails making rss feed code sharing". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Methods:

It is you who piece together the format of RSS XML and output it. And set HTTP Header, mark content-type as application/XML, common code:

# Post_controller::feed () def feed require "rss" articles = Article.find: all,: order = > 'post_date DESC' : limit = > 10 feed = RSS::Maker.make do | maker | maker.channel.title = "Gang of Technology" maker.channel.description = "Gang of Technology site" maker.channel.link = "http://up-u.com" maker.items.do_sort = true articles.each do | article | item = maker.items.new_item item.link =" http://www.***.com/archives/#{article.id}" item.title = article.title item. Date = article.post_date item.description = "" end end send_data feed.to_s : type = > "application/rss+xml",: disposition = > 'inline' end method B: Rails Controller- > Action Code: copy the code as follows: # Post_controller::feed def feed @ posts=Post.find: all,:limit= > "id desc" end erb template: xml.instruct! Xml.rss ("version" = > "2.0"," xmlns:dc "= >" http://purl.org/dc/elements/1.1/") do xml.channel do xml.title "renlu.xu 's blog" xml.link (url_for (: action= > "start") : only_path= > false) xml.description "My life My love" xml.language "zh_CN" xml.ttl 60 for event in @ posts do xml.item do xml.title (event.title) xml.description (event.body) xml.pubDate (event.created_at.to_s (: rfc822)) xml.guid (event.id) xml.link ("http:// …" .. # {event.id} ") this is the content of end end end end" rails making rss feed Code sharing ". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Code production content method more knowledge output practicality learning and then dilemma reality that is common situation article tag format case template Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Linux Shulou Tech Info MariaDB vpn