RoR 101: Session 3

  • 609 views
Uploaded on

Part 3 of 6

Part 3 of 6

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
609
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
8
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Building Web Apps with Rails III
  • 2. Recap● Rails Console● ActiveRecord Queries● Views ● Embedded Ruby Syntax ● Layouts ● Helper Methods
  • 3. Recap: ActiveRecord Models Controllers DB In stations_controller.rb (show):@stations = Station.find(params[:id])
  • 4. The Rails Console1. Open the terminal2. Navigate to the firstfm directory3. Run the commands: rails console @stations = Station.all
  • 5. show.html.erb:<%= link_to Edit, edit_station_path(@station) %> |<%= link_to Back, stations_path %> These are both view helpers.
  • 6. <!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>
  • 7. Web BrowserSession 3 Rails Routing System Views ModelsDB Controllers
  • 8. Web BrowserSession 3: Routing Rails Routing System Views ModelsDB Controllers
  • 9. Web BrowserSession 3:Validations & Testing Rails Routing System Views ModelsDB Controllers
  • 10. Web BrowserSession 3:Associations& The Stream Model Rails Routing System Views Models DB Controllers
  • 11. Routing in Rails
  • 12. Routing in Rails ● Which controller to Web Browser instantiate ● Which action to invoke Parameter(s) to pass ? ●
  • 13. Routing in Rails Web Browser ● Follows routing rules defined in: Routing System ● config/routes.rb Views Controllers
  • 14. Routing in RailsConfig/Routes.rb:Firstfm::Application.routes.draw do resources :stations #commentsend Generates RESTful routes for the Station resource
  • 15. 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
  • 16. Routing in RailsConfig/Routes.rb:Firstfm::Application.routes.draw do resources :stations #commentsend This means we can accept requests for all the CRUD actions
  • 17. Routing in Rails: Terminal Time!1. Open the terminal2. Navigate to the firstfm directory3. Run the commands: rake routes
  • 18. Routing in Rails: Writing RulesConfig/Routes.rb: resources :stations
  • 19. 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
  • 20. Routing in Rails: Writing RulesConfig/Routes.rb: resources :stationsGenerates URL helpers for us e.g. stations_path station_path(:id) new_station_path Etc...
  • 21. 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
  • 22. Tests and Validations
  • 23. We have aproblem...
  • 24. Our app acceptsempty station data
  • 25. Tests and ValidationsSolution: Write validations to prevent erroneous data
  • 26. Tests and ValidationsBut First! Lets write tests.
  • 27. Tests and Validations Scaffolding has generated test files for us
  • 28. 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
  • 29. 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
  • 30. Tests and Validations: Unit TestingRunning test scriptsruby -Itest <script_name>e.g.ruby -Itest test/unit/station_test.rb
  • 31. 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
  • 32. 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
  • 33. 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
  • 34. 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
  • 35. 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
  • 36. Tests and Validations: Validate Example: Using validates_presence_of In models/station.rb:class Station < ActiveRecord::Base validates_presence_of :name, :url,:descriptionend
  • 37. Tests and Validations: Task! Make your tests pass by implementing validations like belowclass Station < ActiveRecord::Base validates_presence_of :name, :url,:descriptionend
  • 38. Associations in RoRAdding Streams
  • 39. First FM Station
  • 40. First FM Station Stream
  • 41. First FM Station Has many Streams
  • 42. 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
  • 43. First FMLets check out our new stream modelrails cmystream = Stream.new
  • 44. First FM Station Stream
  • 45. First FM Station Has many Streams
  • 46. First FMhas_many :streams Station Has many Streams belongs_to :station
  • 47. First FMLets see if our models are associated rails c mystation = Station.first mystation.streams
  • 48. First FM OH NOES! We need to add a secondary key.rails generate migration AddStationIdToStreams station_id:integer
  • 49. First FM OH YEA! Weve got a secondary key!rake db:migraterails cmystation = Station.firstmystation.streams
  • 50. First FM Build Build and save a new streamstream = mystation.streams.build