33
Building Web Apps with Rails IV

RoR 101: Session 4

Embed Size (px)

DESCRIPTION

Part 4 of 6

Citation preview

Page 1: RoR 101: Session 4

Building Web Apps with Rails

IV

Page 2: RoR 101: Session 4

DB

Web Browser

Models

Routing SystemRails

Controllers

Views

Session 3Recap

Page 3: RoR 101: Session 4

DB

Web Browser

Models

Routing SystemRails

Controllers

Views

Session 3: Routing

Page 4: RoR 101: Session 4

DB

Web Browser

Models

Routing SystemRails

Controllers

Views

Session 3: Validations & Testing

Page 5: RoR 101: Session 4

DB

Web Browser

Models

Routing SystemRails

Controllers

Views

Session 3: Associations& The 'Stream' Model

Page 6: RoR 101: Session 4

● Routing● Tests and Validations● Associations

Recap

Page 7: RoR 101: Session 4

Station Streams

First FM

Has many

has_many :streams

belongs_to :station

Page 8: RoR 101: Session 4

● Generating Controllers

● Nesting Resources

● Nesting in Routes● Nesting in Controllers● Nesting in Views

Session 4

Page 9: RoR 101: Session 4

First FM

rails generate controller streamsindex new create destroy

In other words:

rails generate controller <controller name> <actions>

Page 10: RoR 101: Session 4

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

Page 11: RoR 101: Session 4

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

Page 12: RoR 101: Session 4

get "streams/index"

get "streams/new"

get "streams/create"

get "streams/destroy"

resource :streams

Routes.rb

Page 13: RoR 101: Session 4

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.

Page 14: RoR 101: Session 4

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

Page 15: RoR 101: Session 4

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

Page 16: RoR 101: Session 4

streams_controller.rb

def index@streams = Stream.all

end

Page 17: RoR 101: Session 4

streams_controller.rb

def index@streams = Stream.all

end

We want to select a station and get all its streams

Page 18: RoR 101: Session 4

streams_controller.rb

#@streams = Stream.all

@station = Station.find(params[:station_id])

@streams = station.streams

Page 19: RoR 101: Session 4

streams_controller.rb

#@streams = Stream.all

station = Station.find(params[:station_id])

@streams = station.streams

Problem: where are we going to find station_id?

Page 20: RoR 101: Session 4

Nesting Resources

Page 21: RoR 101: Session 4

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

Page 22: RoR 101: Session 4

routes.rb:

Answer: we're going to pass it in the url by nesting resources

get '/stations/:station_id/streams' => 'streams#index'

Page 23: RoR 101: Session 4

routes.rb:

Answer: we're going to pass it in the url by nesting resources

resources :stations do  resources :streams  end

resources :stationsresources :streams

Page 24: RoR 101: Session 4

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

Page 25: RoR 101: Session 4

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.

Page 26: RoR 101: Session 4

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

Page 27: RoR 101: Session 4

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)

Page 28: RoR 101: Session 4

Nested url_helpers

station_streams_path(station)

Used for index and create actions

station_stream_path(station, stream)

new_station_stream_path(station)

Page 29: RoR 101: Session 4

Nested url_helpers

station_streams_path(station)

station_stream_path(station, stream)

Used for show and destroy actions

new_station_stream_path(station)

Page 30: RoR 101: Session 4

Nested url_helpers

station_streams_path(station)

station_stream_path(station, stream)

new_station_stream_path(station)

Used for new action

Page 31: RoR 101: Session 4

Nesting in Views: new.html.erb

<%= form_for @stream do |f| %>

Won't work as it does not include :station_id

Page 32: RoR 101: Session 4

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| %>

Page 33: RoR 101: Session 4

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} %>