creating the application
DESCRIPTION
Agile Web Development with Rails Chapter 6TRANSCRIPT
![Page 1: Creating the application](https://reader036.vdocuments.net/reader036/viewer/2022082916/549a3a74ac79591d2e8b5af5/html5/thumbnails/1.jpg)
Creating the Application
![Page 2: Creating the application](https://reader036.vdocuments.net/reader036/viewer/2022082916/549a3a74ac79591d2e8b5af5/html5/thumbnails/2.jpg)
Creating a Rails application
• rvm --create use 1.8.7@rails3• rails new depot• cd depot• echo ‘rvm use 1.8.7@rails3’ >> .rvmrc• bundle install• rails generate scaffold Product
title:string description:text image_url:string price:decimal
![Page 3: Creating the application](https://reader036.vdocuments.net/reader036/viewer/2022082916/549a3a74ac79591d2e8b5af5/html5/thumbnails/3.jpg)
Rails Migration
• db/migrate/2010031041958_create_products.rb (yours may vary)
![Page 4: Creating the application](https://reader036.vdocuments.net/reader036/viewer/2022082916/549a3a74ac79591d2e8b5af5/html5/thumbnails/4.jpg)
Run Your DB Migrations
• rake db:migrate– Creates the products table– Includes the fields we listed
• rake db:rollback– Reverts last migration– Drops the products table
![Page 5: Creating the application](https://reader036.vdocuments.net/reader036/viewer/2022082916/549a3a74ac79591d2e8b5af5/html5/thumbnails/5.jpg)
Let’s try it
• rails server
• http://localhost:3000/products– Create a new Product– Verify product was created– Reload /products to see list of products– Try deleting a product
![Page 6: Creating the application](https://reader036.vdocuments.net/reader036/viewer/2022082916/549a3a74ac79591d2e8b5af5/html5/thumbnails/6.jpg)
Change view files
• We want to change the number of lines in the description field to 6
• app/views/products/_form.html.erb– Change
<%= f.text_area :description %>to<%= f.text_area :description, :rows => 6 %>
![Page 7: Creating the application](https://reader036.vdocuments.net/reader036/viewer/2022082916/549a3a74ac79591d2e8b5af5/html5/thumbnails/7.jpg)
Run tests
• rake test– You should see 0 failures, 0 errors– We’ll cover this more later
![Page 8: Creating the application](https://reader036.vdocuments.net/reader036/viewer/2022082916/549a3a74ac79591d2e8b5af5/html5/thumbnails/8.jpg)
Add seed data to our DB
• We don’t want to have to create test data manually all the time
• Rails offers db/seeds.rb which will “seed” your database with test data
• curl -o db/seeds.rb http://media.pragprog.com/titles/rails4/code/depot_b/db/seeds.rb
![Page 9: Creating the application](https://reader036.vdocuments.net/reader036/viewer/2022082916/549a3a74ac79591d2e8b5af5/html5/thumbnails/9.jpg)
Grab book images and layout
• http://media.pragprog.com/titles/rails4/code/depot_b/public/images/– Download each image and put it in public/images/
• curl -o public/stylesheets/depot.css http://media.pragprog.com/titles/rails4/code/depot_b/public/stylesheets/depot.css
• rm public/stylesheets/scaffold.css
![Page 10: Creating the application](https://reader036.vdocuments.net/reader036/viewer/2022082916/549a3a74ac79591d2e8b5af5/html5/thumbnails/10.jpg)
Seed our database
• rake db:seed– Runs content in db/seeds.rb
• Reload /products to see our new “seed” books
![Page 11: Creating the application](https://reader036.vdocuments.net/reader036/viewer/2022082916/549a3a74ac79591d2e8b5af5/html5/thumbnails/11.jpg)
Add stylesheet
• System wide layout file
• app/views/layouts/application.html.erb
• <%= stylesheet_link_tag :all %>
![Page 12: Creating the application](https://reader036.vdocuments.net/reader036/viewer/2022082916/549a3a74ac79591d2e8b5af5/html5/thumbnails/12.jpg)
app/views/products/index.html.erb
![Page 13: Creating the application](https://reader036.vdocuments.net/reader036/viewer/2022082916/549a3a74ac79591d2e8b5af5/html5/thumbnails/13.jpg)
Push Depot to Heroku
• Add Heroku to Gemfile
• “bundle install”
• heroku create
• git push heroku master
• heroku rake db:migrate
• heroku rake db:seed
• heroku open (add /products to URL)