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

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

RoR 101: Session 2

900
views

Published on

Part 2 of 6

Part 2 of 6

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
900
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Building Web Apps with Rails II
  • 2. Recap
  • 3. Hello Ruby!Hello Rails
  • 4. Try Ruby! Http://tryruby.org Stop when you see "summary 4" or stuffabout file operations. Skip to challenge on Classes.
  • 5. Our app: FirstFM
  • 6. Scaffoldrails generate scaffold Station name:stringdescription:text url:string
  • 7. Scaffoldrails generate scaffold Station name:stringdescription:text url:string ● Model ● View ● Controller ● Database Migration
  • 8. Anatomy of a Rails App Models, Views, & Controllers Migrations Files
  • 9. CRUD actions in the controller● Create – new, create● Read – index, show● Update – edit, update● Destroy - destroy
  • 10. Session 2● Rails Console● ActiveRecord Queries● Views ● Embedded Ruby Syntax ● Layouts ● Helper Methods
  • 11. Controllers and Models Models ControllersDB
  • 12. Controllers and Models Models ControllersDB In stations_controller.rb (index): @stations = Station.all
  • 13. Controllers and Models Models Controllers DB In stations_controller.rb (show):@stations = Station.find(params[:id])
  • 14. Controllers and Models Models ControllersDB ActiveRecord::BaseMaps station model to stations table in database
  • 15. The Rails Console1. Open the terminal2. Navigate to the firstfm directory3. Run the commands: rails console @stations = Station.all
  • 16. 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
  • 17. 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
  • 18. Views Models ControllersDB@station = Station.find(params[:id])
  • 19. Views Views Models ControllersDB@station = Station.find(params[:id])
  • 20. Views Views Models Controllers DBrespond_to do |format| format.html # index.html.erb format.json {render json: @stations }end
  • 21. View files match action names
  • 22. Where areCreate, update & destroy?
  • 23. These are .erb,embedded ruby files
  • 24. Lets Take A Look!
  • 25. index.html.erb:...<% @stations.each do |station| %>#loop stuff<% end %>... <% %> for code
  • 26. 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
  • 27. show.html.erb:<%= link_to Edit, edit_station_path(@station) %> |<%= link_to Back, stations_path %> Whats this?
  • 28. show.html.erb:<%= link_to Edit, edit_station_path(@station) %> |<%= link_to Back, stations_path %> These are both view helpers.
  • 29. View Helpers & Being DRY Dont Repeat Yourself
  • 30. View Helpers & Being DRYView Helpers:Methods we can callin our views to writeless code / markup!
  • 31. View Helpers & Being DRYRails Provides Us With Helpers:● Links● Forms● Images● And even more!
  • 32. Layouts Layout files are found here
  • 33. <!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>
  • 34. <!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>
  • 35. <!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>
  • 36. 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!
  • 37. Web BrowserHow does itall fit together? Rails Routing System Views ModelsDB Controllers