开发者

How to strip html tags out of xml generated by builder in rails

Here is my situation. I'm trying to create a valid rss feed for an itunes podcast from an existing project. It's a rails application that allows uploading of mp3 radio shows to for listening on the site. I want to take all entries in the database and create an rss feed out of it. I'm using a builder file and everything is working fine. I noticed though that there's html tags stored in the database from a text editor. for example strong tags and span tags are getting put inside of my xml whe开发者_Python百科n the feed is output. Is there a way to strip that stuff out in the controller or in the xml builder?

My controller:

class PodcastsController < ApplicationController
  def index
    @podcasts = Event.find(:all, :order => "date DESC", :conditions => ['radio = ?', 1])
        respond_to do |format|
      format.xml 
    end
  end

end


If the tags are very simple, you could just use a regular expression to strip them out. For example:

"<html>Hello <b>world</b>.</html>".gsub(/<.+?>/, '')
=> "Hello world."

If it's more complex than that, it might make sense to load it in nokogiri and extract the text nodes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜