17. Routing in Rails: Terminal Time!
1. Open the terminal
2. Navigate to the 'firstfm' directory
3. Run the commands:
rake routes
18. Routing in Rails: Writing Rules
Config/Routes.rb:
resources :stations
19. Routing in Rails: Writing Rules
Config/Routes.rb (rewritten):
get 'statations' => 'stations#index'
get 'statations/:id' => 'stations#show'
get 'statations/new' => 'stations#new'
post 'statations' => 'stations#create'
put 'statations/:id' => 'stations#update'
delete 'statations/:id' =>
'stations#destroy'
20. Routing in Rails: Writing Rules
Config/Routes.rb:
resources :stations
Generates URL helpers for us e.g.
stations_path
station_path(:id)
new_station_path
Etc...
21. Routing in Rails: Task
1. Define a root controller and action
2. 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
28. Tests and Validations: Unit Testing
In firstfm/test/unit/station_test.rb:
require 'test_helper'
class StationTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
We write tests by writing out 'assertions'
29. Tests and Validations: Unit Testing
require 'test_helper'
class StationTest < ActiveSupport::TestCase
test "the truth" do
assert 10 > 9
end
end
'assertions' evaluate whether an object is what we expect it to be
30. Tests and Validations: Unit Testing
Running test scripts
ruby -Itest <script_name>
e.g.
ruby -Itest test/unit/station_test.rb
31. Tests and Validations: Unit Testing
ruby -Itest test/unit/station_test.rb
Script report example:
pass: 1, fail: 1, error: 0
total: 2 tests with 2 assertions in 0.215168805
seconds
32. Tests and Validations: Unit Testing
test "shouldnt save without name" do
station = Station.new
station.url = “http://myradio.com”
station.description = “A cool radio
station”
assert !station.save
end
33. Tests and Validations: Unit Testing
test "should save" do
station = Station.new
Station.name = “My Radio”
station.url = “http://myradio.com”
station.description = “A cool radio
station”
assert station.save
end
34. Tests and Validations: Task!
Write test cases for your validations
Consider more than just presence of data e.g.
●
Name shouldn't be longer than x characters
●
URL should start with 'http://'
●
Description shouldn't 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,
:description
end
37. Tests and Validations: Task!
Make your tests pass by implementing validations
like below
class Station < ActiveRecord::Base
validates_presence_of :name, :url,
:description
end
42. First FM
Let's generate the stream model
rails generate model stream
url:string name:string
Create the db table by running the migration script...
rake db:migrate