21.2 Form Helpers 19 Action Controller
edit app/views/products/index.xml.builder
xml.div(class: "productlist") do
xml.timestamp(Time.now)
@products.each do |product|
xml.product do
xml.productname(product.title)
xml.price(product.price, currency: "USD")
end
end
end
edit app/controllers/products_controller.rb
def index
@products = Product.all
respond_to do |format|
format.html
format.xml
end
end
curl --max-time 15 --silent --user dave:secret http://localhost:3000/products.xml
<div class="productlist">
<timestamp>2016-01-20 15:43:12 -0500</timestamp>
<product>
<productname>CoffeeScript</productname>
<price currency="USD">36.0</price>
</product>
<product>
<productname>Programming Ruby 1.9 & 2.0</productname>
<price currency="USD">49.95</price>
</product>
<product>
<productname>Rails Test Prescriptions</productname>
<price currency="USD">34.95</price>
</product>
</div>
irb helpers/date4.rb
Switch to inspect mode.
>> require 'active_support/all'
=> true
>> require 'action_view'
=> true
>> include ActionView::Helpers::DateHelper
=> Object
>> distance_of_time_in_words(Time.now, Time.local(2010, 12, 25))
=> "about 5 years"
>> distance_of_time_in_words(Time.now, Time.now + 33, include_seconds: false)
=> "1 minute"
>> distance_of_time_in_words(Time.now, Time.now + 33, include_seconds: true)
=> "half a minute"
>> time_ago_in_words(Time.local(2009, 12, 25))
=> "about 6 years"
irb helpers/number.rb
Switch to inspect mode.
>> require 'active_support/all'
=> true
>> require 'action_view'
=> true
>> include ActionView::Helpers::NumberHelper
=> Object
>> number_to_currency(123.45)
=> "$123.45"
>> number_to_currency(234.56, :unit => "CAN$", :precision => 0)
=> "CAN$235"
>> number_to_human_size(123_456)
=> "121 KB"
>> number_to_percentage(66.66666)
=> "66.667%"
>> number_to_percentage(66.66666, :precision => 1)
=> "66.7%"
>> number_to_phone(2125551212)
=> "212-555-1212"
>> number_to_phone(2125551212, :area_code => true, :delimiter => " ")
=> "(212) 555 1212"
>> number_with_delimiter(12345678)
=> "12,345,678"
>> number_with_delimiter(12345678, :delimiter => "_")
=> "12_345_678"
>> number_with_precision(50.0/3, :precision => 2)
=> "16.67"