creating the application

13
Creating the Application

Upload: jason-noble

Post on 18-Dec-2014

982 views

Category:

Technology


0 download

DESCRIPTION

Agile Web Development with Rails Chapter 6

TRANSCRIPT

Page 1: Creating the application

Creating the Application

Page 2: Creating the application

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

Rails Migration

• db/migrate/2010031041958_create_products.rb (yours may vary)

Page 4: Creating the application

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

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

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

Run tests

• rake test– You should see 0 failures, 0 errors– We’ll cover this more later

Page 8: Creating the application

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

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

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

Add stylesheet

• System wide layout file

• app/views/layouts/application.html.erb

• <%= stylesheet_link_tag :all %>

Page 12: Creating the application

app/views/products/index.html.erb

Page 13: Creating the application

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)