SlideShare une entreprise Scribd logo
1  sur  84
Rails Bootcamp


Philly.rb     PhillyTechWeek
Mat Schaffer
 mat@mashion.net
   @matschaffer
  matschaffer.com
• Chapter 1: Overview and Installation
• Chapter 2: Data and Views
• Chapter 3: User Stories and Testing
• Chapter 4: Hack time
Chapter 1: Overview

• Why Rails?
• Rails overview
• Dealing with Data
• Live Console and Testing
Why Rails?
Rails Overview
• Developed for BaseCamp by 37 signals
• DSL for web applications
Controller
Controller
Controller




Model
Controller




Model
Controller




Model                View
Controller




Model                View
Convention over
 Configuration
GET /comments

CommentsController#index

 views/comments/index.html.erb
GET /comments/4

CommentsController#show

views/comments/show.html.erb
GET /comments/4.js

CommentsController#show

views/comments/show.js.erb
Data
More Convention over
   Configuration
Super simple
•C   reate


•R   etreive


•U   pdate


•D   elete
Comment.create(:author => "mat")




INSERT INTO comments
(author) VALUES('mat');
Comment.find_by_author("mat")




SELECT * FROM comments
WHERE author = 'mat';
c = Comment.find(1)
c.update_attribute(:author, "mat")




SELECT * FROM comments WHERE id = 1;
UPDATE comments SET author = 'mat' WHERE
id = 1;
c = Comment.find(1)
c.destroy




SELECT * FROM comments WHERE id = 1;
DELETE FROM comments WHERE id = 1;
more data hotness


• Schema management
• Works the same across databases
rails console


• Try out code live
• Inspect your data using code
testing


• Built into rails
• Strong testing culture built into ruby
services

github.com
heroku.com
engineyard.com
RailsHotline.com
Installation
• Mac needs XCode first
  http://developer.apple.com/xcode/


• Linux or Mac, use RVM bootstrap
  http://bit.ly/unixrails


• Windows
  http://railsinstaller.org


• Ubuntu on VirtualBox is fun too
Test your install
rails new kickballapp
cd kickballapp
bundle
rails server
(open a browser to http://localhost:3000)
Our project:
A League Manager
Chapter 2:

  Data

  Views
Data
class Team


• name
• (id, created_at, updated_at)
class Location

• name
• address
• (id, created_at, updated_at)
class Game
• starts_at
• ends_at
• location
• home_team
• away_team
• (id, created_at, updated_at)
class Game
• starts_at
• ends_at                  class Location
• location
• home_team
• away_team
• (id, created_at, updated_at)
class Game
• starts_at
• ends_at                  class Location
• location
• home_team                       class Team
• away_team
• (id, created_at, updated_at)
CODE!
http://bit.ly/railsbc-code
Database Migrations

• live in db/migrate
• ordered
• Abstracted SQL column types
• includes id and timestamps by default
MORE CODE!
RESTful Routes

• GET /locations (show all locations)
• GET /locations/3 (show one location)
• POST /locations (make a location)
• PUT /locations/3 (update one location)
Can also be nested


• GET /locations/3/games (show games for
  one location)
‘public’ folder

• For all static files
• Rails looks here first
• Offers caching options
Views
ERB

• Like JSP/ASP/PHP but in ruby
• lots of helper functions for forms, etc.
• layout → view → partials
VIEW CODE!
ActiveRecord
        Associations

• 1 to 1 (belongs_to - has_one)
• 1 to many (belongs_to - has_many)
• many to many (has_many :through)
Other points


• Controllers
Chapter 3:

User Stories

  Testing
User Stories
(pivotal tracker)
In order to [value]

As a [actor]

I want [feature]
Players feature?
In order to know who is on a team

As a league manager

I want to see a list of players on
the team page
Feature: Player listings
  In order to know who is on a team
  As a league manager
  I want to see a list of players on the team page

  Scenario: listing on a team page
    Given I am on the teams page
    When I follow "Ballshevicks"
    Then I should see "Trotter"
Cucumber
Feature: Player listings
  In order to know who is on a team
  As a league manager
  I want to see a list of players on the team page

  Scenario: listing on a team page
    Given I am on the teams page
    When I follow "Ballshevicks"
    Then I should see "Trotter"
User Stories
as Integration Tests
Setting up Cucumber
1. Add `group :development, :test` to Gemfile
2. Add ‘cucumber-rails’, ‘capybara’ and
   ‘database_cleaner’ in that group
3. Run `bundle`
4. Run `rails generate cucumber:install`
5. `$EDITOR features/players.feature`
undefined local variable or method `node' for
  #<Capybara::Driver::RackTest::Node:...>
               (NameError)

    • Comment out line 18 in features/
      support/env.rb


    • cucumber-rails 0.4.0 fixes this, but it’s still in
      beta
CODE
(setting it up)
Running a scenario


1. Put `@wip` above the scenario
2. Run `rake cucumber:wip`
CODE
(running it)
Debugging

`Then show me the page`
requires ‘launchy’ gem

`Then debug` (debugger;1)
requires ‘ruby-debug’ or ‘ruby-debug19’
CODE
(debugging it)
Building data
`$EDITOR features/support/fixtures.rb`

Before do
  Team.create(:name => "Ballshevicks")
end
• Any features/support/*.rb gets loaded

• `Before` gets run before each Scenario
Other Data Options


• Load test/fixtures/*.yml
• Use the ‘fabrication’ object factory gem
CODE
(giving it data)
Defining Steps
          When I follow "Show" in the row with "Ballshevicks"




When /^I follow "([^"]*)" in the row with "([^"]*)"$/ do |link, text|

  When %Q|I follow "#{link}" within "tr:contains('#{text}')"|

end
               features/step_definitions/table_steps.rb
Defining Steps
          When I follow "Show" in the row with "Ballshevicks"



 Regular Expression


When /^I follow "([^"]*)" in the row with "([^"]*)"$/ do |link, text|

  When %Q|I follow "#{link}" within "tr:contains('#{text}')"|

end
               features/step_definitions/table_steps.rb
Defining Steps
          When I follow "Show" in the row with "Ballshevicks"



 Regular Expression
                             Capturing groups

When /^I follow "([^"]*)" in the row with "([^"]*)"$/ do |link, text|

  When %Q|I follow "#{link}" within "tr:contains('#{text}')"|

end
               features/step_definitions/table_steps.rb
Defining Steps
          When I follow "Show" in the row with "Ballshevicks"



 Regular Expression
                             Capturing groups

When /^I follow "([^"]*)" in the row with "([^"]*)"$/ do |link, text|

  When %Q|I follow "#{link}" within "tr:contains('#{text}')"|

end
               features/step_definitions/table_steps.rb

               Definition in ruby code
web_steps.rb
Given I am on the (rake routes) page
When I press “button” / follow “link”
When I fill in “field” with “value”
When I select “option” from “field”
When I check “field”
Then I should see “text” (within “section”)
CODE
(assigning teams?)
Unit testing
Use for direct testing of

• Models (test/unit/*.rb)
• Helpers (test/unit/helpers/*.rb)
• Controllers (test/functional/*.rb)
Example:
          Player#last_name
class PlayerTest < ActiveSupport::TestCase
  test "parses out last name" do
    trotter = Player.new(:name => "Trotter Cashion")
    assert_equal "Cashion", trotter.last_name
  end
end


               (Test First! At least try.)
CODE
(run and fix it)
Chapter 4:


  Do it!

Contenu connexe

Tendances

Puppet camp chicago-automated_testing2
Puppet camp chicago-automated_testing2Puppet camp chicago-automated_testing2
Puppet camp chicago-automated_testing2
nottings
 
Web development automatisation for fun and profit (Artem Daniliants)
Web development automatisation for fun and profit (Artem Daniliants)Web development automatisation for fun and profit (Artem Daniliants)
Web development automatisation for fun and profit (Artem Daniliants)
LumoSpark
 
Aura Project for PHP
Aura Project for PHPAura Project for PHP
Aura Project for PHP
Hari K T
 

Tendances (20)

Sprockets
SprocketsSprockets
Sprockets
 
Building web framework with Rack
Building web framework with RackBuilding web framework with Rack
Building web framework with Rack
 
Generators
GeneratorsGenerators
Generators
 
OpenERP and Perl
OpenERP and PerlOpenERP and Perl
OpenERP and Perl
 
A tour of Ansible
A tour of AnsibleA tour of Ansible
A tour of Ansible
 
Puppet camp chicago-automated_testing2
Puppet camp chicago-automated_testing2Puppet camp chicago-automated_testing2
Puppet camp chicago-automated_testing2
 
AnsibleFest 2014 - Role Tips and Tricks
AnsibleFest 2014 - Role Tips and TricksAnsibleFest 2014 - Role Tips and Tricks
AnsibleFest 2014 - Role Tips and Tricks
 
Getting started with Ansible
Getting started with AnsibleGetting started with Ansible
Getting started with Ansible
 
Taking Apache Camel For A Ride
Taking Apache Camel For A RideTaking Apache Camel For A Ride
Taking Apache Camel For A Ride
 
V2 and beyond
V2 and beyondV2 and beyond
V2 and beyond
 
Go Web Development
Go Web DevelopmentGo Web Development
Go Web Development
 
Django REST Framework
Django REST FrameworkDjango REST Framework
Django REST Framework
 
Web development automatisation for fun and profit (Artem Daniliants)
Web development automatisation for fun and profit (Artem Daniliants)Web development automatisation for fun and profit (Artem Daniliants)
Web development automatisation for fun and profit (Artem Daniliants)
 
More tips n tricks
More tips n tricksMore tips n tricks
More tips n tricks
 
Using WordPress as your application stack
Using WordPress as your application stackUsing WordPress as your application stack
Using WordPress as your application stack
 
Aura Project for PHP
Aura Project for PHPAura Project for PHP
Aura Project for PHP
 
Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...
Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...
Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...
 
Power of Puppet 4
Power of Puppet 4Power of Puppet 4
Power of Puppet 4
 
Ansible leveraging 2.0
Ansible leveraging 2.0Ansible leveraging 2.0
Ansible leveraging 2.0
 
Spring into rails
Spring into railsSpring into rails
Spring into rails
 

En vedette (11)

Ruby on the Phone
Ruby on the PhoneRuby on the Phone
Ruby on the Phone
 
Node.js
Node.jsNode.js
Node.js
 
wwc start-launched
wwc start-launchedwwc start-launched
wwc start-launched
 
2011 02-08 cucumber
2011 02-08 cucumber2011 02-08 cucumber
2011 02-08 cucumber
 
chef loves windows
chef loves windowschef loves windows
chef loves windows
 
Sfd2012Hanoi Nguyễn Hà Dương - Introduction to Node.js
Sfd2012Hanoi Nguyễn Hà Dương - Introduction to Node.jsSfd2012Hanoi Nguyễn Hà Dương - Introduction to Node.js
Sfd2012Hanoi Nguyễn Hà Dương - Introduction to Node.js
 
Hands On Intro to Node.js
Hands On Intro to Node.jsHands On Intro to Node.js
Hands On Intro to Node.js
 
Knockout vs. angular
Knockout vs. angularKnockout vs. angular
Knockout vs. angular
 
Hadoop a Natural Choice for Data Intensive Log Processing
Hadoop a Natural Choice for Data Intensive Log ProcessingHadoop a Natural Choice for Data Intensive Log Processing
Hadoop a Natural Choice for Data Intensive Log Processing
 
JS Frameworks - Angular Vs Backbone
JS Frameworks - Angular Vs BackboneJS Frameworks - Angular Vs Backbone
JS Frameworks - Angular Vs Backbone
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 

Similaire à PTW Rails Bootcamp

Learning to code for startup mvp session 3
Learning to code for startup mvp session 3Learning to code for startup mvp session 3
Learning to code for startup mvp session 3
Henry S
 
Python & Django TTT
Python & Django TTTPython & Django TTT
Python & Django TTT
kevinvw
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
Fabio Franzini
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
Yehuda Katz
 
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
Nick Sieger
 

Similaire à PTW Rails Bootcamp (20)

Learning to code for startup mvp session 3
Learning to code for startup mvp session 3Learning to code for startup mvp session 3
Learning to code for startup mvp session 3
 
REST APIs in Laravel 101
REST APIs in Laravel 101REST APIs in Laravel 101
REST APIs in Laravel 101
 
Ruby on Rails - Introduction
Ruby on Rails - IntroductionRuby on Rails - Introduction
Ruby on Rails - Introduction
 
Play á la Rails
Play á la RailsPlay á la Rails
Play á la Rails
 
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2
 
APOC Pearls - Whirlwind Tour Through the Neo4j APOC Procedures Library
APOC Pearls - Whirlwind Tour Through the Neo4j APOC Procedures LibraryAPOC Pearls - Whirlwind Tour Through the Neo4j APOC Procedures Library
APOC Pearls - Whirlwind Tour Through the Neo4j APOC Procedures Library
 
Rails 3 (beta) Roundup
Rails 3 (beta) RoundupRails 3 (beta) Roundup
Rails 3 (beta) Roundup
 
Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013
 
Scala in practice - 3 years later
Scala in practice - 3 years laterScala in practice - 3 years later
Scala in practice - 3 years later
 
Scala in-practice-3-years by Patric Fornasier, Springr, presented at Pune Sca...
Scala in-practice-3-years by Patric Fornasier, Springr, presented at Pune Sca...Scala in-practice-3-years by Patric Fornasier, Springr, presented at Pune Sca...
Scala in-practice-3-years by Patric Fornasier, Springr, presented at Pune Sca...
 
Mojolicious
MojoliciousMojolicious
Mojolicious
 
Python & Django TTT
Python & Django TTTPython & Django TTT
Python & Django TTT
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
Crafting Beautiful CLI Applications in Ruby
Crafting Beautiful CLI Applications in RubyCrafting Beautiful CLI Applications in Ruby
Crafting Beautiful CLI Applications in Ruby
 
Rails 101
Rails 101Rails 101
Rails 101
 
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
 
Supa fast Ruby + Rails
Supa fast Ruby + RailsSupa fast Ruby + Rails
Supa fast Ruby + Rails
 
Introduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman OrtegaIntroduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman Ortega
 
Toolbox of a Ruby Team
Toolbox of a Ruby TeamToolbox of a Ruby Team
Toolbox of a Ruby Team
 

PTW Rails Bootcamp

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. What are you interested/concerned about?\n- jobs\n- community involvement\n- notoriety\n- easy\n- fun\n
  7. - Cake PHP, Symphony?\n- Spring Roo, MVC?\n- Django?\n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. Or no database. Use sqlite to get even your designers up and running quickly.\n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n
  73. \n
  74. \n
  75. \n
  76. \n
  77. \n
  78. \n
  79. \n
  80. \n