Agile Web Development with Rails, Edition 4

Agile Web Development with Rails, Edition 4

6.2 Iteration A2: Making Prettier Listings 2 Instant Gratification

6.1 Iteration A1: Creating the Products Maintenance Application

This section mostly covers database configuration options for those users that insist on using MySQL. SQLite3 users will skip most of it.

Create the application.

ruby -rubygems /home/rubys/git/rails/bin/rails new depot
      create  
      create  README
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/helpers/application_helper.rb
      create  app/views/layouts/application.html.erb
      create  app/controllers/application_controller.rb
      create  app/models
      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/test.rb
      create  config/environments/production.rb
      create  config/initializers
      create  config/initializers/mime_types.rb
      create  config/initializers/inflections.rb
      create  config/initializers/session_store.rb
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/secret_token.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  log
      create  log/server.log
      create  log/production.log
      create  log/development.log
      create  log/test.log
      create  public
      create  public/500.html
      create  public/robots.txt
      create  public/favicon.ico
      create  public/422.html
      create  public/404.html
      create  public/index.html
      create  public/images
      create  public/images/rails.png
      create  public/stylesheets
      create  public/stylesheets/.gitkeep
      create  public/javascripts
      create  public/javascripts/prototype.js
      create  public/javascripts/dragdrop.js
      create  public/javascripts/rails.js
      create  public/javascripts/effects.js
      create  public/javascripts/controls.js
      create  public/javascripts/application.js
      create  script
      create  script/rails
      create  test
      create  test/performance/browsing_test.rb
      create  test/test_helper.rb
      create  test/fixtures
      create  test/unit
      create  test/functional
      create  test/integration
      create  tmp
      create  tmp/sessions
      create  tmp/sockets
      create  tmp/cache
      create  tmp/pids
      create  vendor/plugins
      create  vendor/plugins/.gitkeep
bundle install
Using rake (0.8.7) from system gems
Using abstract (1.0.0) from bundler gems
Using activesupport (3.0.0.beta3) from source code at /home/rubys/git/rails
Using builder (2.1.2) from bundler gems
Using i18n (0.4.1) from bundler gems
Using activemodel (3.0.0.beta3) from source code at /home/rubys/git/rails
Using erubis (2.6.5) from bundler gems
Using rack (1.1.0) from bundler gems
Using rack-mount (0.6.3) from bundler gems
Using rack-test (0.5.4) from bundler gems
Using tzinfo (0.3.22) from bundler gems
Using actionpack (3.0.0.beta3) from source code at /home/rubys/git/rails
Using mime-types (1.16) from bundler gems
Using polyglot (0.3.1) from bundler gems
Using treetop (1.4.8) from bundler gems
Using mail (2.2.1) from bundler gems
Using actionmailer (3.0.0.beta3) from source code at /home/rubys/git/rails
Using arel (0.3.3) from bundler gems
Using activerecord (3.0.0.beta3) from source code at /home/rubys/git/rails
Using activeresource (3.0.0.beta3) from source code at /home/rubys/git/rails
Using bundler (0.9.24) from bundler gems
Using thor (0.13.6) from bundler gems
Using railties (3.0.0.beta3) from source code at /home/rubys/git/rails
Using rails (3.0.0.beta3) from source code at /home/rubys/git/rails
Using sqlite3-ruby (1.2.5) from bundler gems
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

Look at the files created.

ls -p
app/	 config.ru  doc/     lib/  public/   README   test/  vendor/
config/  db/	    Gemfile  log/  Rakefile  script/  tmp/

Database configuration options (generally not required for sqlite3)

cat config/database.yml
# SQLite version 3.x
#   gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000
 
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000
 
production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

Generate scaffolding for a real model, modify a template, and do our first bit of data entry.

Generating our first model and associated scaffolding

rails generate scaffold Product title:string description:text image_url:string price:decimal
      invoke  active_record
      create    db/migrate/20100606125202_create_products.rb
      create    app/models/product.rb
      invoke    test_unit
      create      test/unit/product_test.rb
      create      test/fixtures/products.yml
       route  resources :products
      invoke  scaffold_controller
      create    app/controllers/products_controller.rb
      invoke    erb
      create      app/views/products
      create      app/views/products/index.html.erb
      create      app/views/products/edit.html.erb
      create      app/views/products/show.html.erb
      create      app/views/products/new.html.erb
      create      app/views/products/_form.html.erb
      invoke    test_unit
      create      test/functional/products_controller_test.rb
      invoke    helper
      create      app/helpers/products_helper.rb
      invoke      test_unit
      create        test/unit/helpers/products_helper_test.rb
      invoke  stylesheets
      create    public/stylesheets/scaffold.css

Restart the server.

Add precision and scale to the price

edit db/migrate/20100606125202_create_products.rb
class CreateProducts < ActiveRecord::Migration
  def self.up
    create_table :products do |t|
      t.string :title
      t.text :description
      t.string :image_url
      t.decimal :price, :precision => 8, :scale => 2
 
      t.timestamps
    end
  end
 
  def self.down
    drop_table :products
  end
end

Apply the migration

rake db:migrate
mv 20100606125202_create_products.rb 20100301000001_create_products.rb
(in /home/rubys/svn/rails4/Book/util/work-188/depot)
==  CreateProducts: migrating =================================================
-- create_table(:products)
   -> 0.0021s
==  CreateProducts: migrated (0.0022s) ========================================
 

Get an (empty) list of products

get /products

Listing products

Title Description Image url Price

New Product

Show (and modify) one of the templates produced

edit app/views/products/_form.html.erb
<%= form_for(@product) do |f| %>
  <% if @product.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@product.errors.count, "error") %>
      prohibited this product from being saved:</h2>
 
      <ul>
      <% @product.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
 
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_area :description, :rows => 6 %>
  </div>
  <div class="field">
    <%= f.label :image_url %><br />
    <%= f.text_field :image_url %>
  </div>
  <div class="field">
    <%= f.label :price %><br />
    <%= f.text_field :price %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Create a product

get /products/new

New product





Back
post /products
You are being redirected.
get http://localhost:3000/products/1

Product was successfully created.

Title: Web Design for Developers

Description: <p> <em>Web Design for Developers</em> will show you how to make your web-based application look professionally designed. We'll help you learn how to pick the right colors and fonts, avoid costly interface and accessibility mistakes -- your application will really come alive. We'll also walk you through some common Photoshop and CSS techniques and work through a web site redesign, taking a new design from concept all the way to implementation. </p>

Image url: /images/wd4d.jpg

Price: 42.95

Edit | Back

Verify that the product has been added

get /products

Listing products

Title Description Image url Price
Web Design for Developers <p> <em>Web Design for Developers</em> will show you how to make your web-based application look professionally designed. We'll help you learn how to pick the right colors and fonts, avoid costly interface and accessibility mistakes -- your application will really come alive. We'll also walk you through some common Photoshop and CSS techniques and work through a web site redesign, taking a new design from concept all the way to implementation. </p> /images/wd4d.jpg 42.95 Show Edit Destroy

New Product

And, just to verify that we haven't broken anything

rake test
(in /home/rubys/svn/rails4/Book/util/work-188/depot)
Loaded suite /home/rubys/.rvm/gems/ruby-1.8.8-r28169%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
.
Finished in 0.023581 seconds.
 
1 tests, 1 assertions, 0 failures, 0 errors
Loaded suite /home/rubys/.rvm/gems/ruby-1.8.8-r28169%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
.......
Finished in 0.202447 seconds.
 
7 tests, 10 assertions, 0 failures, 0 errors
pub depot_a

6.2 Iteration A2: Making Prettier Listings 2 Instant Gratification