Download - RoR 101: Session 6
![Page 1: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/1.jpg)
Building Web Apps with Rails
VI
![Page 2: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/2.jpg)
Session 5 Recap
● User Model● Installing Gems with Bundler● Authentication with Devise
![Page 3: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/3.jpg)
Session 5: Authentication
At present, anyone can CRUD
(Create Read Update Destroy)
Signed in Users Public
![Page 4: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/4.jpg)
![Page 5: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/5.jpg)
Session 6: Stocking Fillers...
More on Assets
AJAX
Orphans!
Your very own view helper
![Page 6: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/6.jpg)
The Asset Pipeline
NEW in Rails 3.1!
“A framework to concatenate and minify or compress JavaScript and CSS assets.”
Also, enables CoffeeScript, Sass and ERB for assets.
![Page 7: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/7.jpg)
Pre-Processing in Assets
Asset helpers are available e.g.
.class { backgroundimage: url(<%= asset_path 'image.png' %>) }
![Page 8: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/8.jpg)
The Asset Pipeline: Locations
app/assets – your app's assets
vendor/assets – not maintained by you
lib/assets - maintained by you but not app specific
public/ – old skool static location
![Page 9: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/9.jpg)
What assets do we have?
Shows assets available to application including stuff in gems.
Rails.application.config.assets.paths
![Page 10: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/10.jpg)
JS Assets
Go to: 127.0.0.1:3000/assets/application.js
In assets/application.js:
//= require jquery //= require jquery_ujs//= require_tree .
This is a sprockets manifest file.
![Page 11: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/11.jpg)
CSS Assets
Go to: 127.0.0.1:3000/assets/application.css
In assets/application.css:
// *= require_self// *= require_tree .
![Page 12: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/12.jpg)
Cracking into AJAX
![Page 13: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/13.jpg)
AJAX: Step 1
Add
:remote => true
to
link_to 'Destroy', station, confirm: 'Are you sure?', :method => :delete
![Page 14: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/14.jpg)
AJAX: Step 2
Add id to element in view
<tr id = "station_<%= station.id %>">
Respond to javascript in controller
respond_to do |format| format.html { redirect_to stations_url } format.js end
![Page 15: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/15.jpg)
AJAX: Step 3
Keep id of the station to be removed.
@remove_id = @station.id
Define your response javascript: destroy.js.erb
$('#station_<%= @remove_id %>').fadeOut();
![Page 16: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/16.jpg)
Orphans!
It's nearly Christmas, we should help...
![Page 17: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/17.jpg)
Orphans!
BY DESTROYING THEM.
![Page 18: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/18.jpg)
Orphans!
In station.rb:
has_many :streams, :dependent => :destroy
![Page 19: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/19.jpg)
Your Own View Helpers
A Globally Recognized Avatar
![Page 20: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/20.jpg)
Helper Files
These can be found in
“firstfm/app/helpers”
Are included depending on controller.
![Page 21: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/21.jpg)
Adding a Gravatar Helper
require 'digest/md5'
def gravatar_url_for(email) return "http://www.gravatar.com/avatar.php ?gravatar_id= #{Digest::MD5::hexdigest(email)}"end
![Page 22: RoR 101: Session 6](https://reader034.vdocuments.us/reader034/viewer/2022042602/55a2b9b21a28ab445f8b4586/html5/thumbnails/22.jpg)
Helper: Now use it in your view!
Task! Using
<%= gravatar_url_for(email) %>
Display the gravatar for the currently logged in user!