ror 101: session 2
DESCRIPTION
Part 2 of 6TRANSCRIPT
![Page 1: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/1.jpg)
Building Web Apps with Rails
II
![Page 2: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/2.jpg)
Recap
![Page 3: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/3.jpg)
Hello Ruby!
Hello Rails
![Page 4: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/4.jpg)
Try Ruby!
Http://tryruby.org
Stop when you see "summary 4" or stuff
about file operations.
Skip to challenge on Classes.
![Page 5: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/5.jpg)
Our app: FirstFM
![Page 6: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/6.jpg)
Scaffold
rails generate scaffold Station name:string description:text url:string
![Page 7: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/7.jpg)
Scaffold
rails generate scaffold Station name:string description:text url:string
●Model●View●Controller●Database Migration
![Page 8: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/8.jpg)
Anatomy of a Rails App
Models, Views, & Controllers
Migrations Files
![Page 9: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/9.jpg)
● Create – new, create
● Read – index, show
● Update – edit, update
● Destroy - destroy
CRUD actions in the controller
![Page 10: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/10.jpg)
● Rails Console● ActiveRecord Queries
● Views● Embedded Ruby Syntax● Layouts● Helper Methods
Session 2
![Page 11: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/11.jpg)
Controllers and Models
DBModels Controllers
![Page 12: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/12.jpg)
Controllers and Models
DBModels Controllers
In stations_controller.rb (index):
@stations = Station.all
![Page 13: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/13.jpg)
Controllers and Models
DBModels Controllers
In stations_controller.rb (show):
@stations = Station.find(params[:id])
![Page 14: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/14.jpg)
Controllers and Models
DBModels Controllers
ActiveRecord::BaseMaps station model to stations table in database
![Page 15: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/15.jpg)
The Rails Console
1. Open the terminal 2. Navigate to the 'firstfm' directory3. Run the commands:
rails console@stations = Station.all
![Page 16: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/16.jpg)
The Rails Console
rails console@stations = Station.all
Recreate the first line of the show action.
1. Create a hash with the key :id 2. Pass it into Station.find()
Task!
Go to guides.rubyonrails.org/active_record_querying.html
![Page 17: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/17.jpg)
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
![Page 18: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/18.jpg)
DBModels Controllers
Views
@station = Station.find(params[:id])
![Page 19: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/19.jpg)
DBModels Controllers
Views
Views
@station = Station.find(params[:id])
![Page 20: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/20.jpg)
DBModels Controllers
Views
Views
respond_to do |format| format.html # index.html.erb format.json {render json: @stations }end
![Page 21: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/21.jpg)
![Page 22: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/22.jpg)
View files match action names
![Page 23: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/23.jpg)
Where are Create, update & destroy?
![Page 24: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/24.jpg)
These are .erb, embedded ruby files
![Page 25: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/25.jpg)
Let's Take A Look!
![Page 26: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/26.jpg)
...
<% @stations.each do |station| %>
#loop stuff
<% end %>
...
index.html.erb:
<% %> for code
![Page 27: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/27.jpg)
…
<p> <b>Name:</b> <%= @station.name %></p>
<p> <b>Description:</b> <%= @station.description %></p>
<p> <b>Url:</b> <%= @station.url %></p>
...
show.html.erb:
<%= %> for printing
![Page 28: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/28.jpg)
<%= link_to 'Edit', edit_station_path(@station) %> |<%= link_to 'Back', stations_path %>
show.html.erb:
What's this?
![Page 29: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/29.jpg)
<%= link_to 'Edit', edit_station_path(@station) %> |<%= link_to 'Back', stations_path %>
show.html.erb:
These are both view helpers.
![Page 30: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/30.jpg)
View Helpers & Being DRY
Don't Repeat Yourself
![Page 31: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/31.jpg)
View Helpers & Being DRY
View Helpers:
Methods we can call in our views to write less code / markup!
![Page 32: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/32.jpg)
View Helpers & Being DRY
Rails Provides Us With Helpers:
●Links●Forms● Images●And even more!
![Page 33: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/33.jpg)
Layout files are found here
Layouts
![Page 34: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/34.jpg)
<!DOCTYPE html><html><head> <title>Firstfm</title> <%= stylesheet_link_tag "application" %> <%= javascript_include_tag "application" %> <%= csrf_meta_tags %></head><body>
<%= yield %>
</body></html>
The default layouttemplate.
![Page 35: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/35.jpg)
<!DOCTYPE html><html><head> <title>Firstfm</title> <%= stylesheet_link_tag "application" %> <%= javascript_include_tag "application" %> <%= csrf_meta_tags %></head><body>
<%= yield %>
</body></html>
This is where our templates
are rendered
![Page 36: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/36.jpg)
<!DOCTYPE html><html><head> <title>Firstfm</title> <%= stylesheet_link_tag "application" %> <%= javascript_include_tag "application" %> <%= csrf_meta_tags %></head><body>
<%= yield %>
</body></html>
This helpers include Our default js / css
![Page 37: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/37.jpg)
Task: Display a logo image on each page !
1. Find and download an image to use as a logo
2. 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!
![Page 38: RoR 101: Session 2](https://reader034.vdocuments.us/reader034/viewer/2022052411/5564925dd8b42a5f6c8b4f59/html5/thumbnails/38.jpg)
DB
Web Browser
Models
Routing SystemRails
Controllers
Views
How does it all fit together?