Agile Web Development with Rails, Edition 4

25.1 rack 21.2 Form Helpers

24.3 Active Resources

Turn off strict sanitization

edit config/environments/development.rb
Depot::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb
 
  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false
 
  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true
 
  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false
 
  # Don't care if the mailer can't send
  config.action_mailer.raise_delivery_errors = false
 
  # Don't actually send emails
  config.action_mailer.delivery_method = :test
  #
  # Alternate configuration example, using gmail:
  #   config.action_mailer.delivery_method = :smtp
  #   config.action_mailer.smtp_settings = {
  #     address:        "smtp.gmail.com",
  #     port:           587, 
  #     domain:         "domain.of.sender.net",
  #     authentication: "plain",
  #     user_name:      "dave",
  #     password:       "secret",
  #     enable_starttls_auto: true
  #   } 
 
  # Print deprecation notices to the Rails logger
  config.active_support.deprecation = :log
 
  # Only use best-standards-support built into browsers
  config.action_dispatch.best_standards_support = :builtin
 
  # Raise exception on mass assignment protection for Active Record models
  config.active_record.mass_assignment_sanitizer = :logger
 
  # Log the query plan for queries taking more than this (works
  # with SQLite, MySQL, and PostgreSQL)
  config.active_record.auto_explain_threshold_in_seconds = 0.5
 
  # Do not compress assets
  config.assets.compress = false
 
  # Expands the lines which load the assets
  config.assets.debug = true
end

Restart the server.

bundle exec /home/rubys/git/rails/railties/bin/rails new depot_client --skip-bundle
      create  
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/images/rails.png
      create  app/assets/javascripts/application.js
      create  app/assets/stylesheets/application.css
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/mailers
      create  app/models
      create  app/views/layouts/application.html.erb
      create  app/mailers/.gitkeep
      create  app/models/.gitkeep
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/secret_token.rb
      create  config/initializers/session_store.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  doc
      create  doc/README_FOR_APP
      create  lib
      create  lib/tasks
      create  lib/tasks/.gitkeep
      create  lib/assets
      create  lib/assets/.gitkeep
      create  log
      create  log/.gitkeep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/index.html
      create  public/robots.txt
      create  script
      create  script/rails
      create  test/fixtures
      create  test/fixtures/.gitkeep
      create  test/functional
      create  test/functional/.gitkeep
      create  test/integration
      create  test/integration/.gitkeep
      create  test/unit
      create  test/unit/.gitkeep
      create  test/performance/browsing_test.rb
      create  test/test_helper.rb
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor/assets/javascripts
      create  vendor/assets/javascripts/.gitkeep
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.gitkeep
      create  vendor/plugins
      create  vendor/plugins/.gitkeep
bundle install --local
Resolving dependencies...
Using rake 10.4.2
Using i18n 0.7.0
Using multi_json 1.11.0
Using activesupport 3.2.21 from source at /home/rubys/git/rails
Using builder 3.0.4
Using activemodel 3.2.21 from source at /home/rubys/git/rails
Using erubis 2.7.0
Using journey 1.0.4.20120614141756 from source at /home/rubys/git/journey
Using rack 1.4.5
Using rack-cache 1.2
Using rack-test 0.6.3
Using hike 1.2.3
Using tilt 1.4.1
Using sprockets 2.2.3
Using actionpack 3.2.21 from source at /home/rubys/git/rails
Using mime-types 1.25.1
Using polyglot 0.3.5
Using treetop 1.4.15
Using mail 2.5.4
Using actionmailer 3.2.21 from source at /home/rubys/git/rails
Using arel 3.0.3.20131114190737 from source at /home/rubys/git/arel
Using tzinfo 0.3.44
Using activerecord 3.2.21 from source at /home/rubys/git/rails
Using activeresource 3.2.21 from source at /home/rubys/git/rails
Using bundler 1.9.9
Using coffee-script-source 1.9.1.1
Using execjs 2.5.2
Using coffee-script 2.4.1
Using rack-ssl 1.3.4
Using json 1.8.2
Using rdoc 3.12.2
Using thor 0.19.1
Using railties 3.2.21 from source at /home/rubys/git/rails
Using coffee-rails 3.2.2 from source at /home/rubys/git/coffee-rails
Using jquery-rails 3.1.2
Using rails 3.2.21 from source at /home/rubys/git/rails
Using sass 3.4.14
Using sass-rails 3.2.6 from source at /home/rubys/git/sass-rails
Using sqlite3 1.3.10
Using uglifier 2.7.1
Bundle complete! 8 Gemfile dependencies, 40 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
edit app/models/product.rb
  class Product < ActiveResource::Base
    self.site = 'http://dave:secret@localhost:3000/'
  end
echo "Product.find(2).title" | IRBRC=tmp/irbrc rails console
Loading development environment (Rails 3.2.21)
Switch to inspect mode.
>> Product.find(2).title
=> "CoffeeScript"
>> 
edit app/controllers/line_items_controller.rb
  def create
      if params[:line_item]
        # ActiveResource
        params[:line_item][:order_id] = params[:order_id]
        @line_item = LineItem.new(params[:line_item])
      else
        # HTML forms
      product = Product.find(params[:product_id])
      @line_item = @cart.add_product(product.id)
      end
    @line_item.product = product
 
    respond_to do |format|
      if @line_item.save
        format.html { redirect_to store_url }
        format.js   { @current_item = @line_item }
        format.json { render json: @line_item,
          status: :created, location: @line_item }
      else
        format.html { render action: "new" }
        format.json { render json: @line_item.errors,
          status: :unprocessable_entity }
      end
    end
  end
edit config/routes.rb
Depot::Application.routes.draw do
  get 'admin' => 'admin#index'
  controller :sessions do
    get  'login' => :new
    post 'login' => :create
    delete 'logout' => :destroy
  end
 
  resources :users
  resources :products do
    get :who_bought, on: :member
  end
 
  scope '(:locale)' do
    resources :orders do
      resources :line_items
    end
 
    resources :line_items
    resources :carts
    root to: 'store#index', as: 'store'
  end
end
echo "Product.find(2).title" | IRBRC=tmp/irbrc rails console
Loading development environment (Rails 3.2.21)
Switch to inspect mode.
>> Product.find(2).title
=> "CoffeeScript"
>> 
echo "p = Product.find(2)\\nputs p.price\\np.price = BigDecimal.new(p.price)-5\\np.save" | IRBRC=tmp/irbrc rails console
Loading development environment (Rails 3.2.21)
Switch to inspect mode.
>> p = Product.find(2)
=> #<Product:0x00000004569fb0 @attributes={"created_at"=>"2015-05-26T04:40:32Z", "description"=>"<p>\n        CoffeeScript is JavaScript done right. It provides all of JavaScript's\n\tfunctionality wrapped in a cleaner, more succinct syntax. In the first\n\tbook on this exciting new language, CoffeeScript guru Trevor Burnham\n\tshows you how to hold onto all the power and flexibility of JavaScript\n\twhile writing clearer, cleaner, and safer code.\n      </p>", "id"=>2, "image_url"=>"cs.jpg", "price"=>"36.0", "title"=>"CoffeeScript", "updated_at"=>"2015-05-26T04:40:32Z"}, @prefix_options={}, @persisted=true>
>> puts p.price
36.0
=> nil
>> p.price = BigDecimal.new(p.price)-5
=> #<BigDecimal:455d008,'0.31E2',9(18)>
>> p.save
=> true
>> 

expire cache

rm -rf /home/rubys/git/awdwr/edition4/work-32/depot/tmp/cache

fetch storefront

get /

Your Pragmatic Catalog

Cs

CoffeeScript

CoffeeScript is JavaScript done right. It provides all of JavaScript's functionality wrapped in a cleaner, more succinct syntax. In the first book on this exciting new language, CoffeeScript guru Trevor Burnham shows you how to hold onto all the power and flexibility of JavaScript while writing clearer, cleaner, and safer code.

$31.00
Ruby

Programming Ruby 1.9 & 2.0

Ruby is the fastest growing and most exciting dynamic language out there. If you need to get working programs delivered fast, you should add Ruby to your toolbox.

$49.95
Rtp

Rails Test Prescriptions

Rails Test Prescriptions is a comprehensive guide to testing Rails applications, covering Test-Driven Development from both a theoretical perspective (why to test) and from a practical perspective (how to test effectively). It covers the core Rails testing tools and procedures for Rails 2 and Rails 3, and introduces popular add-ons, including Cucumber, Shoulda, Machinist, Mocha, and Rcov.

$34.95

fetch product (fallback in case storefront is cached)

get /login
Please Log In
post /login?locale=en
You are being redirected.
get http://localhost:3000/admin?locale=en

Welcome

It's 2015-05-26 00:42:57 -0400 We have 2 orders.
get /products/2

Title: CoffeeScript

Description: <p> CoffeeScript is JavaScript done right. It provides all of JavaScript's functionality wrapped in a cleaner, more succinct syntax. In the first book on this exciting new language, CoffeeScript guru Trevor Burnham shows you how to hold onto all the power and flexibility of JavaScript while writing clearer, cleaner, and safer code. </p>

Image url: cs.jpg

Price: 31.0

Edit | Back
edit app/models/order.rb
  class Order < ActiveResource::Base
    self.site = 'http://dave:secret@localhost:3000/'
  end
echo "Order.find(1).name\\nOrder.find(1).line_items\\n" | IRBRC=tmp/irbrc rails console
Loading development environment (Rails 3.2.21)
Switch to inspect mode.
>> Order.find(1).name
=> "Dave Thomas"
>> Order.find(1).line_items
NoMethodError: undefined method `line_items' for #<Order:0x00000003d93890>
	from /home/rubys/git/rails/activeresource/lib/active_resource/base.rb:1470:in `method_missing'
	from (irb):2
	from /home/rubys/git/rails/railties/lib/rails/commands/console.rb:47:in `start'
	from /home/rubys/git/rails/railties/lib/rails/commands/console.rb:8:in `start'
	from /home/rubys/git/rails/railties/lib/rails/commands.rb:41:in `<top (required)>'
	from script/rails:6:in `require'
	from script/rails:6:in `<main>'
>> 
edit app/models/line_item.rb
  class LineItem < ActiveResource::Base
    self.site = 'http://dave:secret@localhost:3000/orders/:order_id'
  end
post /logout?locale=en
You are being redirected.
get http://localhost:3000/en

Logged out

Your Pragmatic Catalog

Cs

CoffeeScript

CoffeeScript is JavaScript done right. It provides all of JavaScript's functionality wrapped in a cleaner, more succinct syntax. In the first book on this exciting new language, CoffeeScript guru Trevor Burnham shows you how to hold onto all the power and flexibility of JavaScript while writing clearer, cleaner, and safer code.

$31.00
Ruby

Programming Ruby 1.9 & 2.0

Ruby is the fastest growing and most exciting dynamic language out there. If you need to get working programs delivered fast, you should add Ruby to your toolbox.

$49.95
Rtp

Rails Test Prescriptions

Rails Test Prescriptions is a comprehensive guide to testing Rails applications, covering Test-Driven Development from both a theoretical perspective (why to test) and from a practical perspective (how to test effectively). It covers the core Rails testing tools and procedures for Rails 2 and Rails 3, and introduces popular add-ons, including Cucumber, Shoulda, Machinist, Mocha, and Rcov.

$34.95
echo "LineItem.find(:all, :params => {:order_id=>1})" | IRBRC=tmp/irbrc rails console
Loading development environment (Rails 3.2.21)
Switch to inspect mode.
>> LineItem.find(:all, :params => {:order_id=>1})
=> [#<LineItem:0x000000037e5998 @attributes={"cart_id"=>nil, "created_at"=>"2015-05-26T04:41:25Z", "id"=>10, "price"=>"36.0", "product_id"=>2, "quantity"=>1, "updated_at"=>"2015-05-26T04:41:36Z"}, @prefix_options={:order_id=>1}, @persisted=true>, #<LineItem:0x000000037e4430 @attributes={"cart_id"=>nil, "created_at"=>"2015-05-26T04:42:27Z", "id"=>11, "price"=>"36.0", "product_id"=>2, "quantity"=>2, "updated_at"=>"2015-05-26T04:42:28Z"}, @prefix_options={:order_id=>1}, @persisted=true>]
>> 
get /orders/1/line_items.json
[{"cart_id":null,"created_at":"2015-05-26T04:41:25Z","id":10,"order_id":1,"price":"36.0","product_id":2,"quantity":1,"updated_at":"2015-05-26T04:41:36Z"},{"cart_id":null,"created_at":"2015-05-26T04:42:27Z","id":11,"order_id":2,"price":"36.0","product_id":2,"quantity":2,"updated_at":"2015-05-26T04:42:28Z"}]
edit app/controllers/line_items_controller.rb
  def index
    @line_items = LineItem.all
 
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @line_items }
        format.xml { render xml: @line_items }
    end
  end
get /orders/1/line_items.xml
<?xml version="1.0" encoding="UTF-8"?>
<line-items type="array">
  <line-item>
    <cart-id type="integer" nil="true"/>
    <created-at type="datetime">2015-05-26T04:41:25Z</created-at>
    <id type="integer">10</id>
    <order-id type="integer">1</order-id>
    <price type="decimal">36.0</price>
    <product-id type="integer">2</product-id>
    <quantity type="integer">1</quantity>
    <updated-at type="datetime">2015-05-26T04:41:36Z</updated-at>
  </line-item>
  <line-item>
    <cart-id type="integer" nil="true"/>
    <created-at type="datetime">2015-05-26T04:42:27Z</created-at>
    <id type="integer">11</id>
    <order-id type="integer">2</order-id>
    <price type="decimal">36.0</price>
    <product-id type="integer">2</product-id>
    <quantity type="integer">2</quantity>
    <updated-at type="datetime">2015-05-26T04:42:28Z</updated-at>
  </line-item>
</line-items>
echo "LineItem.format = :xml\\nli = LineItem.find(:all, :params => {:order_id=>1}).first\\nputs li.price\\nli.price *= 0.8\\nli.save" | IRBRC=tmp/irbrc rails console
Loading development environment (Rails 3.2.21)
Switch to inspect mode.
>> LineItem.format = :xml
=> :xml
>> li = LineItem.find(:all, :params => {:order_id=>1}).first
=> #<LineItem:0x00000003006718 @attributes={"cart_id"=>nil, "created_at"=>2015-05-26 04:41:25 UTC, "id"=>10, "price"=>#<BigDecimal:3005480,'0.36E2',9(18)>, "product_id"=>2, "quantity"=>1, "updated_at"=>2015-05-26 04:41:36 UTC}, @prefix_options={:order_id=>1}, @persisted=true>
>> puts li.price
36.0
=> nil
>> li.price *= 0.8
=> #<BigDecimal:2fdea88,'0.288E2',18(27)>
>> li.save
ActiveResource::ClientError: Failed.  Response code = 406.  Response message = Not Acceptable .
	from /home/rubys/git/rails/activeresource/lib/active_resource/connection.rb:146:in `handle_response'
	from /home/rubys/git/rails/activeresource/lib/active_resource/connection.rb:115:in `request'
	from /home/rubys/git/rails/activeresource/lib/active_resource/connection.rb:92:in `block in put'
	from /home/rubys/git/rails/activeresource/lib/active_resource/connection.rb:218:in `with_auth'
	from /home/rubys/git/rails/activeresource/lib/active_resource/connection.rb:92:in `put'
	from /home/rubys/git/rails/activeresource/lib/active_resource/base.rb:1353:in `update'
	from /home/rubys/git/rails/activeresource/lib/active_resource/observing.rb:19:in `update_with_notifications'
	from /home/rubys/git/rails/activeresource/lib/active_resource/base.rb:1155:in `save'
	from /home/rubys/git/rails/activeresource/lib/active_resource/validations.rb:79:in `save_with_validation'
	from /home/rubys/git/rails/activeresource/lib/active_resource/observing.rb:19:in `save_with_notifications'
	from (irb):5
	from /home/rubys/git/rails/railties/lib/rails/commands/console.rb:47:in `start'
	from /home/rubys/git/rails/railties/lib/rails/commands/console.rb:8:in `start'
	from /home/rubys/git/rails/railties/lib/rails/commands.rb:41:in `<top (required)>'
	from script/rails:6:in `require'
	from script/rails:6:in `<main>'
>> 
echo "li2 = LineItem.new(:order_id=>1, :product_id=>2, :quantity=>1, :price=>0.0)\\nli2.save" | IRBRC=tmp/irbrc rails console
Loading development environment (Rails 3.2.21)
Switch to inspect mode.
>> li2 = LineItem.new(:order_id=>1, :product_id=>2, :quantity=>1, :price=>0.0)
=> #<LineItem:0x00000003de33e0 @attributes={"product_id"=>2, "quantity"=>1, "price"=>0.0}, @prefix_options={:order_id=>1}, @persisted=false>
>> li2.save
=> true
>> 

25.1 rack 21.2 Form Helpers