Generating RSS feed in Rails 3

前端 未结 2 1257
情话喂你
情话喂你 2021-01-29 18:08

I\'m looking for a best practice/standard pattern for generating feeds in Rails 3. Is http://railscasts.com/episodes/87-generating-rss-feeds still valid?

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-29 18:42

    I did something similar but without creating a new action.

    index.atom.builder

    atom_feed :language => 'en-US' do |feed|
      feed.title "Articles"
      feed.updated Time.now
    
      @articles.each do |item|
        next if item.published_at.blank?
    
        feed.entry( item ) do |entry|
          entry.url article_url(item)
          entry.title item.title
          entry.content item.content, :type => 'html'
    
          # the strftime is needed to work with Google Reader.
          entry.updated(item.published_at.strftime("%Y-%m-%dT%H:%M:%SZ")) 
          entry.author item.user.handle
        end
      end
    end
    

    You don't need to do anything special in the controller unless you have some special code like i did. For example I'm using the will_paginate gem and for the atom feed I don't want it to paginate so I did this to avoid that.

    controller

      def index
        if current_user && current_user.admin?
          @articles = Article.paginate :page => params[:page], :order => 'created_at DESC'
        else
          respond_to do |format|
            format.html { @articles = Article.published.paginate :page => params[:page], :order => 'published_at DESC' }
            format.atom { @articles = Article.published }
          end
        end
      end
    

提交回复
热议问题