search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Table of Contents
Creating RSS Feeds in Rails
1. Add a route for the feed
2. Create the controller action
3. Create the RSS view template
? Consuming External RSS Feeds
1. Add the gem
2. Fetch and parse a feed
3. Store or display the data
Bonus Tips
Home Backend Development XML/RSS Tutorial Creating and Consuming RSS Feeds in a Ruby on Rails Application

Creating and Consuming RSS Feeds in a Ruby on Rails Application

Sep 13, 2025 am 12:04 AM
rss

要在Ruby on Rails中创建RSS订阅源,需先在routes.rb中添加rss路由,再在控制器中定义rss动作并渲染无布局的RSS模板,最后用builder文件编写符合规范的XML结构;2. 消费外部RSS则需引入feedjira gem,通过Feedjira.parse解析远程feed内容,并可结合Rails.cache缓存提升性能;两者均无需外部API,直接利用Rails内置XML支持与标准RSS格式即可完成。

Creating and Consuming RSS Feeds in a Ruby on Rails Application

If you're building a Ruby on Rails app and want to expose content via RSS (like blog posts or updates), or consume external RSS feeds, it's actually straightforward — especially since Rails has built-in support for generating XML feeds. Here's how to do both.

Creating and Consuming RSS Feeds in a Ruby on Rails Application

Creating RSS Feeds in Rails

This is perfect if you want to let users subscribe to your blog, news, or any regularly updated content.

1. Add a route for the feed

In config/routes.rb:

Creating and Consuming RSS Feeds in a Ruby on Rails Application
resources :posts do
  collection do
    get :rss
  end
end

2. Create the controller action

In app/controllers/posts_controller.rb:

def rss
  @posts = Post.published.limit(20).order(created_at: :desc)

  respond_to do |format|
    format.rss { render layout: false }
  end
end

3. Create the RSS view template

Make a file at app/views/posts/rss.rss.builder:

xml.instruct! :xml, version: "1.0"
xml.rss version: "2.0" do
  xml.channel do
    xml.title "Your App Blog"
    xml.description "Latest posts from our blog"
    xml.link root_url

    @posts.each do |post|
      xml.item do
        xml.title post.title
        xml.description post.body
        xml.pubDate post.created_at.to_s(:rfc822)
        xml.link post_url(post)
        xml.guid post_url(post)
      end
    end
  end
end

? Tip: Use .to_s(:rfc822) for proper RSS date formatting. Also, make sure your URLs are absolute (use _url helpers, not _path).

Now your feed is live at /posts.rss.


? Consuming External RSS Feeds

To pull in and parse feeds from elsewhere (like news sources or partner blogs), use the feedjira gem — it’s reliable and handles many feed formats.

1. Add the gem

In your Gemfile:

gem 'feedjira'

Then run bundle install.

2. Fetch and parse a feed

Example in a controller or service object:

require 'feedjira'

feed_url = "https://example.com/feed.rss"
feed = Feedjira.parse(open(feed_url).read)

feed.entries.each do |entry|
  puts entry.title
  puts entry.url
  puts entry.published
end

3. Store or display the data

You might want to:

  • Cache the feed (use Rails.cache.fetch with a TTL)
  • Store entries in a model like ExternalFeedItem
  • Display them in a view (e.g., "Latest from Industry News")

Example caching:

@external_posts = Rails.cache.fetch('external_feed', expires_in: 1.hour) do
  feed = Feedjira.parse(open('https://example.com/feed.rss').read)
  feed.entries.first(10)
end

Bonus Tips

  • For SEO and discoverability, add this to your layout’s <head>:
    <%= auto_discovery_link_tag(:rss, posts_rss_path, title: 'RSS Feed') %>
  • Test your feed with W3C Feed Validation Service
  • If you have multiple content types (e.g., videos + articles), consider separate feeds per type.

  • That’s it! You can now both publish and consume RSS in your Rails app — no external APIs needed for basic use cases. Just clean, standard XML and a few helpers.

    The above is the detailed content of Creating and Consuming RSS Feeds in a Ruby on Rails Application. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP and XML to implement RSS subscription management and display on the website How to use PHP and XML to implement RSS subscription management and display on the website Jul 29, 2023 am 10:09 AM

How to use PHP and XML to implement RSS subscription management and display on a website. RSS (Really Simple Syndication) is a standard format for publishing frequently updated blog posts, news, audio and video content. Many websites provide RSS subscription functions, allowing users to easily obtain the latest information. In this article, we will learn how to use PHP and XML to implement the RSS subscription management and display functions of the website. First, we need to create an RSS subscription to XM

XML/RSS Data Integration: Practical Guide for Developers & Architects XML/RSS Data Integration: Practical Guide for Developers & Architects Apr 02, 2025 pm 02:12 PM

XML/RSS data integration can be achieved by parsing and generating XML/RSS files. 1) Use Python's xml.etree.ElementTree or feedparser library to parse XML/RSS files and extract data. 2) Use ElementTree to generate XML/RSS files and gradually add nodes and data.

RSS Document Tools: Building, Validating, and Publishing Feeds RSS Document Tools: Building, Validating, and Publishing Feeds Apr 09, 2025 am 12:10 AM

How to build, validate and publish RSSfeeds? 1. Build: Use Python scripts to generate RSSfeed, including title, link, description and release date. 2. Verification: Use FeedValidator.org or Python script to check whether RSSfeed complies with RSS2.0 standards. 3. Publish: Upload RSS files to the server, or use Flask to generate and publish RSSfeed dynamically. Through these steps, you can effectively manage and share content.

Building Feeds with XML: A Hands-On Guide to RSS Building Feeds with XML: A Hands-On Guide to RSS Apr 14, 2025 am 12:17 AM

The steps to build an RSSfeed using XML are as follows: 1. Create the root element and set the version; 2. Add the channel element and its basic information; 3. Add the entry element, including the title, link and description; 4. Convert the XML structure to a string and output it. With these steps, you can create a valid RSSfeed from scratch and enhance its functionality by adding additional elements such as release date and author information.

XML/RSS Deep Dive: Mastering Parsing, Validation, and Security XML/RSS Deep Dive: Mastering Parsing, Validation, and Security Apr 03, 2025 am 12:05 AM

The parsing, verification and security of XML and RSS can be achieved through the following steps: parsing XML/RSS: parsing RSSfeed using Python's xml.etree.ElementTree module to extract key information. Verify XML: Use the lxml library and XSD schema to verify the validity of XML documents. Ensure security: Use the defusedxml library to prevent XXE attacks and protect the security of XML data. These steps help developers efficiently process and protect XML/RSS data, improving work efficiency and data security.

Is There an RSS Alternative Based on JSON? Is There an RSS Alternative Based on JSON? Apr 10, 2025 am 09:31 AM

JSONFeed is a JSON-based RSS alternative that has its advantages simplicity and ease of use. 1) JSONFeed uses JSON format, which is easy to generate and parse. 2) It supports dynamic generation and is suitable for modern web development. 3) Using JSONFeed can improve content management efficiency and user experience.

Beyond the Basics: Advanced RSS Document Features Beyond the Basics: Advanced RSS Document Features Apr 21, 2025 am 12:03 AM

Advanced features of RSS include content namespaces, extension modules, and conditional subscriptions. 1) Content namespace extends RSS functionality, 2) Extended modules such as DublinCore or iTunes to add metadata, 3) Conditional subscription filters entries based on specific conditions. These functions are implemented by adding XML elements and attributes to improve information acquisition efficiency.

PHP application: Get rss subscription content through function PHP application: Get rss subscription content through function Jun 20, 2023 pm 06:25 PM

With the rapid development of the Internet, more and more websites have begun to provide RSS subscription services, allowing users to easily obtain updated content from the website. As a popular server-side scripting language, PHP has many functions for processing RSS subscriptions, allowing developers to easily extract the required data from RSS sources. This article will introduce how to use PHP functions to obtain RSS subscription content. 1. What is RSS? The full name of RSS is "ReallySimpleSyndication" (abbreviated

Related articles