Upload
rory-gianni
View
933
Download
2
Embed Size (px)
DESCRIPTION
Part 2 of 6
Citation preview
Building Web Apps with Rails
II
Recap
Hello Ruby!
Hello Rails
Try Ruby!
Http://tryruby.org
Stop when you see "summary 4" or stuff
about file operations.
Skip to challenge on Classes.
Our app: FirstFM
Scaffold
rails generate scaffold Station name:string description:text url:string
Scaffold
rails generate scaffold Station name:string description:text url:string
●Model●View●Controller●Database Migration
Anatomy of a Rails App
Models, Views, & Controllers
Migrations Files
● Create – new, create
● Read – index, show
● Update – edit, update
● Destroy - destroy
CRUD actions in the controller
● Rails Console● ActiveRecord Queries
● Views● Embedded Ruby Syntax● Layouts● Helper Methods
Session 2
Controllers and Models
DBModels Controllers
Controllers and Models
DBModels Controllers
In stations_controller.rb (index):
@stations = Station.all
Controllers and Models
DBModels Controllers
In stations_controller.rb (show):
@stations = Station.find(params[:id])
Controllers and Models
DBModels Controllers
ActiveRecord::BaseMaps station model to stations table in database
The Rails Console
1. Open the terminal 2. Navigate to the 'firstfm' directory3. Run the commands:
rails console@stations = Station.all
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
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
DBModels Controllers
Views
@station = Station.find(params[:id])
DBModels Controllers
Views
Views
@station = Station.find(params[:id])
DBModels Controllers
Views
Views
respond_to do |format| format.html # index.html.erb format.json {render json: @stations }end
View files match action names
Where are Create, update & destroy?
These are .erb, embedded ruby files
Let's Take A Look!
...
<% @stations.each do |station| %>
#loop stuff
<% end %>
...
index.html.erb:
<% %> for code
…
<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
<%= link_to 'Edit', edit_station_path(@station) %> |<%= link_to 'Back', stations_path %>
show.html.erb:
What's this?
<%= link_to 'Edit', edit_station_path(@station) %> |<%= link_to 'Back', stations_path %>
show.html.erb:
These are both view helpers.
View Helpers & Being DRY
Don't Repeat Yourself
View Helpers & Being DRY
View Helpers:
Methods we can call in our views to write less code / markup!
View Helpers & Being DRY
Rails Provides Us With Helpers:
●Links●Forms● Images●And even more!
Layout files are found here
Layouts
<!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.
<!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
<!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
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!
DB
Web Browser
Models
Routing SystemRails
Controllers
Views
How does it all fit together?