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

RoR 101: Session 4

on

  • 482 views

Part 4 of 6

Part 4 of 6

Statistics

Views

Total Views
482
Views on SlideShare
482
Embed Views
0

Actions

Likes
0
Downloads
6
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 4 RoR 101: Session 4 Presentation Transcript

    • Building Web Apps with Rails IV
    • Web BrowserSession 3Recap 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
    • Recap● Routing● Tests and Validations● Associations
    • First FMhas_many :streams Station Has many Streams belongs_to :station
    • Session 4● Generating Controllers● Nesting Resources ● Nesting in Routes ● Nesting in Controllers ● Nesting in Views
    • First FMrails generate controller streamsindex new create destroyIn 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
    • Routes.rbget "streams/index"get "streams/new" resource :streamsget "streams/create"get "streams/destroy"
    • streams_controller.rbclass StreamsController < ApplicationController  def index  end  def new  end Nobodys home!  def create Your mission is to  end fill out the controller & views.  def destroy  endend
    • 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.rbdef index @streams = Stream.allend
    • streams_controller.rbdef index @streams = Stream.allend 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.allstation = Station.find(params[:station_id])@streams = station.streams Problem: where are we going to find station_id?
    • Nesting Resources
    • Answer: were going to pass it in the url by nesting resourceshttp://127.0.0.1:3000/stations/1/streams Were going to tell the routes to interpret this as :station_id
    • Answer: were going to pass it in the url by nesting resourcesroutes.rb:get /stations/:station_id/streams => streams#index
    • Answer: were going to pass it in the url by nesting resourcesroutes.rb:resources :stationsresources :streamsresources :stations do  resources :streams  end
    • Answer: were going to pass it in the url by nesting resourcesroutes.rb:resources :stationsresources :streams rake routesresources :stations do  resources :streams  end
    • streams_controller.rbdef 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.rbdef new @station = Station.find(params[:station_id]) @stream = station.streams.buildenddef create station = Station.find(params[:station_id]) @stream = station.streams.build(params[:stream]) ...end
    • Nested Resources and Viewsroutes.rb: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_helpersstation_streams_path(station)Used for index and create actionsstation_stream_path(station, stream)new_station_stream_path(station)
    • Nested url_helpersstation_streams_path(station)station_stream_path(station, stream)Used for show and destroy actionsnew_station_stream_path(station)
    • Nested url_helpersstation_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| %> Wont work as it does not include :station_id
    • Nesting in Views: new.html.erb<%= form_for @stream do |f| %> Wont work as its 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} %>