RoR 101: Session 3
Upcoming SlideShare
Loading in...5
×
 

RoR 101: Session 3

on

  • 789 views

Part 3 of 6

Part 3 of 6

Statistics

Views

Total Views
789
Slideshare-icon Views on SlideShare
789
Embed Views
0

Actions

Likes
0
Downloads
8
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial LicenseCC Attribution-NonCommercial License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    RoR 101: Session 3 RoR 101: Session 3 Presentation Transcript

    • Building Web Apps with Rails III
    • Recap● Rails Console● ActiveRecord Queries● Views ● Embedded Ruby Syntax ● Layouts ● Helper Methods
    • Recap: ActiveRecord Models Controllers DB In stations_controller.rb (show):@stations = Station.find(params[:id])
    • The Rails Console1. Open the terminal2. Navigate to the firstfm directory3. Run the commands: rails console @stations = Station.all
    • show.html.erb:<%= link_to Edit, edit_station_path(@station) %> |<%= link_to Back, stations_path %> These are both view helpers.
    • <!DOCTYPE html><html><head> <title>Firstfm</title> <%= stylesheet_link_tag "application" %> <%= javascript_include_tag "application"%> <%= csrf_meta_tags %></head><body><%= yield %> The default layout template.</body></html>
    • Web BrowserSession 3 Rails Routing System Views ModelsDB Controllers
    • Web BrowserSession 3: Routing Rails Routing System Views ModelsDB Controllers
    • Web BrowserSession 3:Validations & Testing Rails Routing System Views ModelsDB Controllers
    • Web BrowserSession 3:Associations& The Stream Model Rails Routing System Views Models DB Controllers
    • Routing in Rails
    • Routing in Rails ● Which controller to Web Browser instantiate ● Which action to invoke Parameter(s) to pass ? ●
    • Routing in Rails Web Browser ● Follows routing rules defined in: Routing System ● config/routes.rb Views Controllers
    • Routing in RailsConfig/Routes.rb:Firstfm::Application.routes.draw do resources :stations #commentsend Generates RESTful routes for the Station resource
    • Routing in RailsConfig/Routes.rb:Firstfm::Application.routes.draw do resources :stationsTransfer Principles: Representational State #comments Use HTTP methods explicitlyend Be stateless Expose directory structure-like URIs Handle multiple formats e.g. XML, JSON
    • Routing in RailsConfig/Routes.rb:Firstfm::Application.routes.draw do resources :stations #commentsend This means we can accept requests for all the CRUD actions
    • Routing in Rails: Terminal Time!1. Open the terminal2. Navigate to the firstfm directory3. Run the commands: rake routes
    • Routing in Rails: Writing RulesConfig/Routes.rb: resources :stations
    • Routing in Rails: Writing RulesConfig/Routes.rb (rewritten):get statations => stations#indexget statations/:id => stations#showget statations/new => stations#newpost statations => stations#createput statations/:id => stations#updatedelete statations/:id =>stations#destroy
    • Routing in Rails: Writing RulesConfig/Routes.rb: resources :stationsGenerates URL helpers for us e.g. stations_path station_path(:id) new_station_path Etc...
    • Routing in Rails: Task1. Define a root controller and action2. Add a new action 2.1 Write a new method in the controller 2.2 Write a view file for it 2.3 Write a routing rule so we can access it For docs on routing: guides.rubyonrails.org/routing.html
    • Tests and Validations
    • We have aproblem...
    • Our app acceptsempty station data
    • Tests and ValidationsSolution: Write validations to prevent erroneous data
    • Tests and ValidationsBut First! Lets write tests.
    • Tests and Validations Scaffolding has generated test files for us
    • Tests and Validations: Unit TestingIn firstfm/test/unit/station_test.rb:require test_helperclass StationTest < ActiveSupport::TestCase  # test "the truth" do  #   assert true  # endend We write tests by writing out assertions
    • Tests and Validations: Unit Testingrequire test_helperclass StationTest < ActiveSupport::TestCase  test "the truth" do     assert 10 > 9  endendassertions evaluate whether an object is what we expect it to be
    • Tests and Validations: Unit TestingRunning test scriptsruby -Itest <script_name>e.g.ruby -Itest test/unit/station_test.rb
    • Tests and Validations: Unit Testingruby -Itest test/unit/station_test.rbScript report example:pass: 1, fail: 1, error: 0total: 2 tests with 2 assertions in 0.215168805seconds
    • Tests and Validations: Unit Testingtest "shouldnt save without name" do station = Station.new station.url = “http://myradio.com” station.description = “A cool radio station” assert !station.saveend
    • Tests and Validations: Unit Testingtest "should save" do station = Station.new Station.name = “My Radio” station.url = “http://myradio.com” station.description = “A cool radio station” assert station.saveend
    • Tests and Validations: Task!Write test cases for your validationsConsider more than just presence of data e.g.● Name shouldnt be longer than x characters● URL should start with http://● Description shouldnt be less than x characters
    • Tests and Validations: Validate● We write validation code in the model● Rails provides us with several built in validation methods● For example: validates_presence_of
    • Tests and Validations: Validate Example: Using validates_presence_of In models/station.rb:class Station < ActiveRecord::Base validates_presence_of :name, :url,:descriptionend
    • Tests and Validations: Task! Make your tests pass by implementing validations like belowclass Station < ActiveRecord::Base validates_presence_of :name, :url,:descriptionend
    • Associations in RoRAdding Streams
    • First FM Station
    • First FM Station Stream
    • First FM Station Has many Streams
    • First FMLets generate the stream model rails generate model stream url:string name:stringCreate the db table by running the migration script... rake db:migrate
    • First FMLets check out our new stream modelrails cmystream = Stream.new
    • First FM Station Stream
    • First FM Station Has many Streams
    • First FMhas_many :streams Station Has many Streams belongs_to :station
    • First FMLets see if our models are associated rails c mystation = Station.first mystation.streams
    • First FM OH NOES! We need to add a secondary key.rails generate migration AddStationIdToStreams station_id:integer
    • First FM OH YEA! Weve got a secondary key!rake db:migraterails cmystation = Station.firstmystation.streams
    • First FM Build Build and save a new streamstream = mystation.streams.build