Question

I can't seem to get the syntax correct for adding categories to blog posts.

index.atom.builder

atom_feed language: 'en-US', schema_date: 2013 do |feed|
  feed.title @title
  feed.updated @updated

  @posts.each do |post|
    next if post.updated_at.blank?
    feed.entry post, published: post.published_at do |entry|
      entry.title post.title
      entry.summary post.summary.blank? ? truncate("#{strip_tags(post.content)}", length: 140, separator: ' ') : strip_tags(post.summary)
      entry.content post.content, type: 'html'
      entry.author do |author|
        author.name post.user ? post.user.name : post.author
      end
      post.categories.map {|c| c.name}.each do |t|
        entry.category term: t, label: t, scheme: 'http://mywebsite.com'
      end
    end # end feed.entry
  end # end @posts.each
end

Answered all my own questions. For anyone hitting this, this will give you a validated atom feed! Passing the published: value in the |entry| block is only necessary if you don't want to use the default created_at and updated_at values. Check your feed at http://validator.w3.org/feed/ to check yours!

Was it helpful?

Solution

This is what I ended up using. This actually gets 2 different models into the feed.

views/entries/feed.atom.builder

atom_feed language: 'en-US', schema_date: 2013 do |feed|
  feed.title @title
  feed.updated @updated

  @entries.each do |e|
    next if e.updated_at.blank?
    feed.entry e do |entry|
      entry.title e.name
      entry.summary e.summary.blank? ? truncate("#{strip_tags(e.content)}", length: 140, separator: ' ') : strip_tags(e.summary), type: 'html'
      entry.content e.content, type: 'html'
      entry.author do |author|
        author.name e.user.name
      end
      e.categories.map {|c| c.name}.each do |t|
        entry.category term: t, label: t, scheme: root_url
      end
      entry.category term: 'blog', label: 'blog', scheme: root_url
    end # end feed.entry
  end # end @entries.each

  @designs.each do |d|
    next if d.updated_at.blank?
    feed.entry d do |entry|
      entry.title d.name
      entry.summary truncate("#{strip_tags(d.content)}", length: 140, separator: ' '), type: 'html'
      entry.content d.content, type: 'html'
      entry.author do |author|
        author.name User.first.name
      end
      d.categories.map {|c| c.name}.each do |t|
        entry.category term: t, label: t, scheme: root_url
      end
      entry.category term: 'design', label: 'design', scheme: root_url
    end # end feed.entry
  end # end @designs.each
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top