简体   繁体   中英

Creating a RSS feed with Rails

I'm trying to create an RSS feed for my news posts, I've googled it and come up with this code:

def feed
  @posts = News.all(:conditions => "#{Settings.show} = 1", :select => "id, title, heading, content, date_posted", :order => "date_posted DESC") 

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

then in a file called "feed.rss.builder" I have this:

xml.instruct! :xml, :version => "1.0" 
xml.rss :version => "2.0" do
  xml.channel do
    xml.title "Your Blog Title"
    xml.description "A blog about software and chocolate"
    xml.link posts_url

    for post in @posts
      xml.item do
        xml.title post.title
        xml.description post.content
        xml.pubDate post.date_posted.to_s(:rfc822)
        xml.link post_url(post)
        xml.guid post_url(post)
       end
    end
  end
end

I've added it into my routes file match "/news/feed" => "aboutus#feed" but when I go to that page nothing is rendered..

This was the code I ended up with:

def news

@news = News.find(:all, :order => "date_posted desc", :conditions => "#{Settings.show} = 1")
render :template => 'about-us/news/feed.rss.builder', :layout => false

end

and:

xml.instruct! :xml, :version => "1.0" 
xml.rss :version => "2.0" do
  xml.channel do
    xml.title "News"
    xml.description "Description"
    xml.link "/news"

for post in @news
  xml.item do
    xml.title post.title
    xml.description post.content
    xml.pubDate post.date_posted.to_s(:rfc822)
    xml.link "/news/#{post.reference}"
    xml.guid "/news/#{post.reference}"
    xml.icon "/favicon.ico"
   end
end
end
end

Is it possible that it's working correctly, but that your browser isn't displaying the RSS?

You could try using curl in terminal to see if anything is being rendered:

curl http://localhost:3000/news/feed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM