从Rails向Atom提要添加类别

时间:2013-09-09 17:44:17

标签: ruby-on-rails ruby-on-rails-3 rss atom-feed

我似乎无法正确地将语法添加到博客帖子中。

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

回答了我自己的所有问题。对于任何人来说,这将为您提供经过验证的原子进给!只有在您不想使用默认的published:|entry|值时,才需要在created_at块中传递updated_at值。检查您的Feed http://validator.w3.org/feed/以查看您的Feed!

1 个答案:

答案 0 :(得分:1)

这是我最终使用的。这实际上有2个不同的模型进入饲料。

<强>视图/条目/ 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