4-3 atom订阅源

时间:2023-01-31 23:45:36

部分章节还没有实做练习。

网上购买了安道的Rails指南中文版。联系了这个作者问了一个问题Rails5的翻译问题。


try(), 判断是否存在,不存在的话返回nil.例子:pasting

@person.try(:name) instead of: @person.name if @person

atom订阅:

atom_feed()

atom_feed do |feed|
  feed.title "Who bought #{@product.title}"
  feed.updated(@latest_order.try(:updated_at))
  @product.orders.each do |order|
    feed.entry(order) do |entry|
      entry.title "Order #{order.id}"
      entry.summary type: 'xhtml' do |xhtml|
        xhtml.p "shipped to #{order.address}"
        xhtml.table do
          xhtml.tr do
            xhtml.th "Product"
            xhtml.th "Quantity"
            xhtml.th "Total Price"
          end
          order.line_items.each do |item|
            xhtml.tr do
              xhtml.td item.product.title
              xhtml.td item.quantity
              xhtml.td number_to_currency item.total_price
            end
          end
          xhtml.tr do
            xhtml.th 'total', colspan: 2
            xhtml.th number_to_currency(order.line_items.map(&:total_price).sum)
          end
        end
        entry.author do |author|
          author.name order.name
          author.email(order.email)
        end
      end
    end
  end
end

输出:curl --silent http://localhost:3000/products/1/who_bought.atom

得到xml格式的,可以在自己喜欢的阅读器上订阅这个Atom源。

<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:localhost,2005:/products/1/who_bought</id>
  <link rel="alternate" type="text/html" href="http://localhost:3000"/>
  <link rel="self" type="application/atom+xml" href="http://localhost:3000/products/1/who_bought.atom"/>
  <title>Who bought Rails, Angular, Postgres, and Bootstrap</title>
  <updated>2018-04-02T13:19:44Z</updated>
  <entry>
    <id>tag:localhost,2005:Order/2</id>
    <published>2018-04-02T13:19:44Z</published>
    <updated>2018-04-02T13:19:44Z</updated>
    <link rel="alternate" type="text/html" href="http://localhost:3000/orders/2"/>
    <title>Order 2</title>
    <summary type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>shipped to beijing</p>
        <table>
          <tr>
            <th>Product</th>
            <th>Quantity</th>
            <th>Total Price</th>
          </tr>
          <tr>
            <td>Rails, Angular, Postgres, and Bootstrap</td>
            <td>4</td>
            <td>$440.00</td>
          </tr>
          <tr>
            <th colspan="2">total</th>
            <th>$440.00</th>
          </tr>
        </table>
        <author>
          <name>wangming</name>
          <email>wangming@google.com</email>
        </author>
      </div>
    </summary>
  </entry>
</feed>