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
      format.json { render json: @products }
    end
  end
curl --max-time 15 --silent --user dave:secret http://localhost:3000/products.xml
<div class="productlist">
  <timestamp>2015-05-26 00:42:44 -0400</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
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))
=> "over 4 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))
=> "over 5 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"
 

21.2 Form Helpers 20.1 Testing Routes