zoukankan      html  css  js  c++  java
  • 4-3 atom订阅源

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

    网上购买了安道的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>


  • 相关阅读:
    datepicker防手动输入
    [ACM]Link-Cut Tree实现动态树初探
    STL priority_queue 优先队列 小记
    hihoCoder挑战赛1 毁灭者问题
    python编程技巧
    openstack horizon 学习(3) DataTable
    Upcasting, downcasting in JAVA
    SGU 145
    URAL 1003,1004
    自建物流的无人机实验(困难)
  • 原文地址:https://www.cnblogs.com/chentianwei/p/8709882.html
Copyright © 2011-2022 走看看