0
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        ...
Our app: FirstFM
Scaffoldrails generate scaffold Station name:stringdescription:text url:string
Scaffoldrails generate scaffold Station name:stringdescription:text url:string            ● Model            ● View       ...
Anatomy of a Rails App                         Models, Views, & Controllers                                         Migrat...
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):     @statio...
Controllers and Models                Models             Controllers DB        In stations_controller.rb (show):@stations ...
Controllers and Models                Models            ControllersDB              ActiveRecord::BaseMaps station model to...
The Rails Console1. Open the terminal2. Navigate to the firstfm directory3. Run the commands:   rails console   @stations ...
The Rails Console    rails console    @stations = Station.all                       Task!  Recreate the first line of the ...
The Rails Console  Recreate the first line of the show action.  1. Create a hash with the key :id  2. Pass it into Station...
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 # ind...
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 ...
show.html.erb:…<p>  <b>Name:</b>  <%= @station.name %></p><p>  <b>Description:</b>  <%= @station.description %></p><p>  <b...
show.html.erb:<%= link_to Edit, edit_station_path(@station) %> |<%= link_to Back, stations_path %>                        ...
show.html.erb:<%= link_to Edit, edit_station_path(@station) %> |<%= link_to Back, stations_path %>    These are both view ...
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 "ap...
<!DOCTYPE html><html><head> <title>Firstfm</title> <%= stylesheet_link_tag "application" %> <%= javascript_include_tag "ap...
<!DOCTYPE html><html><head> <title>Firstfm</title> <%= stylesheet_link_tag "application" %> <%= javascript_include_tag "ap...
Task: Display a logo image on eachpage !1. Find and download an image to use asa logo2. Put it in firstfm/public/assets/3....
Web BrowserHow does itall fit together?          Rails                              Routing System                        ...
RoR 101: Session 2
Upcoming SlideShare
Loading in...5
×

RoR 101: Session 2

929

Published on

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
929
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "RoR 101: Session 2"

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

    Clipping is a handy way to collect important slides you want to go back to later.

×