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

RoR 101: Session 2

on

  • 1,042 views

Part 2 of 6

Part 2 of 6

Statistics

Views

Total Views
1,042
Views on SlideShare
1,042
Embed Views
0

Actions

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

  • Building Web Apps with Rails II
  • Recap
  • Hello Ruby!Hello Rails
  • Try Ruby! Http://tryruby.org Stop when you see "summary 4" or stuffabout file operations. Skip to challenge on Classes.
  • Our app: FirstFM
  • Scaffoldrails generate scaffold Station name:stringdescription:text url:string
  • Scaffoldrails generate scaffold Station name:stringdescription:text url:string ● Model ● View ● Controller ● Database Migration
  • Anatomy of a Rails App Models, Views, & Controllers Migrations Files
  • CRUD actions in the controller● Create – new, create● Read – index, show● Update – edit, update● Destroy - destroy
  • Session 2● Rails Console● ActiveRecord Queries● Views ● Embedded Ruby Syntax ● Layouts ● Helper Methods
  • Controllers and Models Models ControllersDB
  • Controllers and Models Models ControllersDB In stations_controller.rb (index): @stations = Station.all
  • Controllers and Models Models Controllers DB In stations_controller.rb (show):@stations = Station.find(params[:id])
  • Controllers and Models Models ControllersDB ActiveRecord::BaseMaps station model to stations table in database
  • The Rails Console1. Open the terminal2. Navigate to the firstfm directory3. Run the commands: rails console @stations = Station.all
  • The Rails Console rails console @stations = Station.all Task! Recreate the first line of the show action. 1. Create a hash with the key :id 2. Pass it into Station.find() Go toguides.rubyonrails.org/active_record_querying.html
  • The Rails Console Recreate the first line of the show action. 1. Create a hash with the key :id 2. Pass it into Station.find() Try out other AR methods:guides.rubyonrails.org/active_record_querying.html apidock.com/rails/ActiveRecord/Base
  • Views Models ControllersDB@station = Station.find(params[:id])
  • Views Views Models ControllersDB@station = Station.find(params[:id])
  • Views Views Models Controllers DBrespond_to do |format| format.html # index.html.erb format.json {render json: @stations }end
  • View files match action names
  • Where areCreate, update & destroy?
  • These are .erb,embedded ruby files
  • Lets Take A Look!
  • index.html.erb:...<% @stations.each do |station| %>#loop stuff<% end %>... <% %> for code
  • show.html.erb:…<p>  <b>Name:</b>  <%= @station.name %></p><p>  <b>Description:</b>  <%= @station.description %></p><p>  <b>Url:</b>  <%= @station.url %></p> <%= %>... for printing
  • show.html.erb:<%= link_to Edit, edit_station_path(@station) %> |<%= link_to Back, stations_path %> Whats this?
  • show.html.erb:<%= link_to Edit, edit_station_path(@station) %> |<%= link_to Back, stations_path %> These are both view helpers.
  • View Helpers & Being DRY Dont Repeat Yourself
  • View Helpers & Being DRYView Helpers:Methods we can callin our views to writeless code / markup!
  • View Helpers & Being DRYRails Provides Us With Helpers:● Links● Forms● Images● And even more!
  • Layouts Layout files are found here
  • <!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>
  • <!DOCTYPE html><html><head> <title>Firstfm</title> <%= stylesheet_link_tag "application" %> <%= javascript_include_tag "application"%> <%= csrf_meta_tags %></head><body> This is where our<%= yield %> templates are rendered</body></html>
  • <!DOCTYPE html><html><head> <title>Firstfm</title> <%= stylesheet_link_tag "application" %> <%= javascript_include_tag "application"%> <%= csrf_meta_tags %></head><body><%= yield %> This helpers include Our default js / css</body></html>
  • Task: Display a logo image on eachpage !1. Find and download an image to use asa logo2. Put it in firstfm/public/assets/3. Display it with the image_tag helper Search for image_tag on http://apidock.com/rails for reference!
  • Web BrowserHow does itall fit together? Rails Routing System Views ModelsDB Controllers