Agile Web Development with Rails, Edition 4

21.2 Form Helpers 20.1 Testing Routes

21.1 Views

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 # index.html.erb
      format.xml  # index.xml.builder
    end
  end
curl --max-time 15 --silent --user dave:secret http://localhost:3000/products.xml
<div class="productlist">
  <timestamp>Tue Feb 04 14:53:02 -0500 2014</timestamp>
  <product>
    <productname>CoffeeScript</productname>
    <price currency="USD">36.0</price>
  </product>
  <product>
    <productname>Programming Ruby 1.9 &amp; 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/date3.rb
>> 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))
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
=> "about 3 years"
>> distance_of_time_in_words(Time.now, Time.now + 33, false)
=> "1 minute"
>> distance_of_time_in_words(Time.now, Time.now + 33, true)
=> "half a minute"
>> time_ago_in_words(Time.local(2009, 12, 25))
=> "about 4 years"
 
irb helpers/number.rb
>> require 'active_support/all'
=> true
>> require 'action_view'
=> true
>> include ActionView::Helpers::NumberHelper
=> Object
>> number_to_currency(123.45)
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
=> "$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"
 

21.2 Form Helpers 20.1 Testing Routes