Generowanie kanału RSS w Rails 3

Szukam najlepszej praktyki / standardowego wzorca generowania kanałów w Rails 3. Is http://railscasts.com/episodes/87-generating-rss-feeds nadal aktualne?

Author: shingara, 2011-01-28

2 answers

Po pierwsze, obecnie zalecam używanie ATOM feed zamiast RSS.

Specyfikacja ATOM feed oferuje więcej wartości niż RSS jeden z internacjonalizacji, typy treści i inne rzeczy i każdy nowoczesny czytnik kanałów obsługuje go.

Więcej informacji o ATOM vs RSS można znaleźć na stronie:


Na kodowanie:

Ten przykład zakłada:

  • model o nazwie NewsItem z następującymi atrybutami:
    • title
    • content
    • author_name
  • kontroler dla tego modelu (news_items_controller.rb), do którego dodasz feed akcję

Użyjemy do tego szablonu Buildera i Ruby on Rails atom_feed helper , który jest bardzo przydatny.

1. Dodaj akcję do kontroler

Przejdź do app/controllers/news_items_controller.rb i dodaj:

def feed
  # this will be the name of the feed displayed on the feed reader
  @title = "FEED title"

  # the news items
  @news_items = NewsItem.order("updated_at desc")

  # this will be our Feed's update timestamp
  @updated = @news_items.first.updated_at unless @news_items.empty?

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

    # we want the RSS feed to redirect permanently to the ATOM feed
    format.rss { redirect_to feed_path(:format => :atom), :status => :moved_permanently }
  end
end

2. Konfiguracja szablonu builder

Teraz dodajmy szablon, aby zbudować kanał.

Przejdź do app/views/news_items/feed.atom.builder i dodaj:

atom_feed :language => 'en-US' do |feed|
  feed.title @title
  feed.updated @updated

  @news_items.each do |item|
    next if item.updated_at.blank?

    feed.entry( item ) do |entry|
      entry.url news_item_url(item)
      entry.title item.title
      entry.content item.content, :type => 'html'

      # the strftime is needed to work with Google Reader.
      entry.updated(item.updated_at.strftime("%Y-%m-%dT%H:%M:%SZ")) 

      entry.author do |author|
        author.name entry.author_name
      end
    end
  end
end

3. Połącz go z trasą

Udostępnijmy kanały na http://domain.com/feed

Spowoduje to wywołanie akcji w formacie ATOM domyślnie i przekierowanie /feed.rss na /feed.atom.

Przejdź do config/routes.rb i dodaj:

resources :news_items
match '/feed' => 'news_items#feed',
      :as => :feed,
      :defaults => { :format => 'atom' }

4. Dodaj link do Atom i kanałów RSS na layout

Na koniec pozostaje tylko dodać kanał do układu.

Przejdź do app/views/layouts/application.html.erb i dodaj ten dział <head></head>:

<%= auto_discovery_link_tag :atom, "/feed" %>
<%= auto_discovery_link_tag :rss, "/feed.rss" %>

W tym może być literówka lub dwie, więc daj mi znać, jeśli to zadziała.

 114
Author: tomeduarte,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-08 20:19:56

Zrobiłem coś podobnego, ale bez tworzenia nowej akcji.

Indeks.atom.budowniczy

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

Nie musisz robić niczego specjalnego w kontrolerze, chyba że masz jakiś specjalny kod, tak jak ja. Na przykład używam kleju will_paginate i dla kanału atom nie chcę go paginować, więc zrobiłem to, aby tego uniknąć.

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
 10
Author: holden,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-20 09:12:55