RoR 101: Session 4
-
Upload
rory-gianni -
Category
Technology
-
view
419 -
download
1
description
Transcript of RoR 101: Session 4
Building Web Apps with Rails
IV
DB
Web Browser
Models
Routing SystemRails
Controllers
Views
Session 3Recap
DB
Web Browser
Models
Routing SystemRails
Controllers
Views
Session 3: Routing
DB
Web Browser
Models
Routing SystemRails
Controllers
Views
Session 3: Validations & Testing
DB
Web Browser
Models
Routing SystemRails
Controllers
Views
Session 3: Associations& The 'Stream' Model
● Routing● Tests and Validations● Associations
Recap
Station Streams
First FM
Has many
has_many :streams
belongs_to :station
● Generating Controllers
● Nesting Resources
● Nesting in Routes● Nesting in Controllers● Nesting in Views
Session 4
First FM
rails generate controller streamsindex new create destroy
In other words:
rails generate controller <controller name> <actions>
rails generate controller streams index new create destroy:
create app/controllers/streams_controller.rb
route get "streams/destroy" route get "streams/create" route get "streams/new" route get "streams/index"
create app/views/streams create app/views/streams/index.html.erb create app/views/streams/new.html.erb create app/views/streams/create.html.erb create app/views/streams/destroy.html.erb
rails generate controller streams index new create destroy:
create app/controllers/streams_controller.rb
route get "streams/destroy" route get "streams/create" route get "streams/new" route get "streams/index"
create app/views/streams create app/views/streams/index.html.erb create app/views/streams/new.html.erb create app/views/streams/create.html.erb create app/views/streams/destroy.html.erb
get "streams/index"
get "streams/new"
get "streams/create"
get "streams/destroy"
resource :streams
Routes.rb
streams_controller.rb
class StreamsController < ApplicationController
def index end
def new end
def create end
def destroy end
end
Nobody's home!
Your mission is to fill out the controller
& views.
StreamsController should...
● Index - show all streams
● New - show the form to create a stream
● Create - take form data and saves a stream
● Destroy - delete a stream from the database
StreamsController should really...
● Index - show all streams belonging to a station
● New - show the form to create a stream belonging to a station
● Create - take form data and saves a stream belonging to a station
● Destroy - delete a stream from the database
streams_controller.rb
def index@streams = Stream.all
end
streams_controller.rb
def index@streams = Stream.all
end
We want to select a station and get all its streams
streams_controller.rb
#@streams = Stream.all
@station = Station.find(params[:station_id])
@streams = station.streams
streams_controller.rb
#@streams = Stream.all
station = Station.find(params[:station_id])
@streams = station.streams
Problem: where are we going to find station_id?
Nesting Resources
Answer: we're going to pass it in the url by nesting resources
http://127.0.0.1:3000/stations/1/streams
We're going to tell the routes to interpretthis as :station_id
routes.rb:
Answer: we're going to pass it in the url by nesting resources
get '/stations/:station_id/streams' => 'streams#index'
routes.rb:
Answer: we're going to pass it in the url by nesting resources
resources :stations do resources :streams end
resources :stationsresources :streams
routes.rb:
Answer: we're going to pass it in the url by nesting resources
resources :stations do resources :streams end
resources :stationsresources :streams
rake routes
streams_controller.rb
def index @station = Station.find(params[:station_id]) @streams = station.streamsend
Like 'streams#index'Our controllers and views will have to
reflect this change.
streams_controller.rb
def new @station = Station.find(params[:station_id]) @stream = station.streams.buildend
def create station = Station.find(params[:station_id]) @stream = station.streams.build(params[:stream]) ...end
routes.rb:
Nested Resources and Views
resources :stations do resources :streams end
Generates url_helpers e.g.
station_streams_path(station)station_stream_path(station, stream)new_station_stream_path(station)
Nested url_helpers
station_streams_path(station)
Used for index and create actions
station_stream_path(station, stream)
new_station_stream_path(station)
Nested url_helpers
station_streams_path(station)
station_stream_path(station, stream)
Used for show and destroy actions
new_station_stream_path(station)
Nested url_helpers
station_streams_path(station)
station_stream_path(station, stream)
new_station_stream_path(station)
Used for new action
Nesting in Views: new.html.erb
<%= form_for @stream do |f| %>
Won't work as it does not include :station_id
Nesting in Views: new.html.erb
<%= form_for @stream do |f| %>
Won't work as it's path does not include :station_id
<%= form_for @stream, :url => station_streams_path(@station) do |f| %>
Connecting up the Views
Too many pages?
Create a partial called '_streams.html.erb'
This will be the same as index.html.erb
Include it in 'stations/show.html.erb':
<%= render :partial => “streams/streams”, :locals => {:streams => @streams} %>