ror 101: session 6

Post on 13-Jul-2015

318 Views

Category:

Technology

5 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Building Web Apps with Rails

VI

Session 5 Recap

● User Model● Installing Gems with Bundler● Authentication with Devise

Session 5: Authentication

At present, anyone can CRUD

(Create Read Update Destroy)

Signed in Users Public

Session 6: Stocking Fillers...

More on Assets

AJAX

Orphans!

Your very own view helper

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.

Pre-Processing in Assets

Asset helpers are available e.g.

.class { background­image: url(<%= asset_path 'image.png' %>) }

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

What assets do we have?

Shows assets available to application including stuff in gems.

Rails.application.config.assets.paths

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.

CSS Assets

Go to: 127.0.0.1:3000/assets/application.css

In assets/application.css:

// *= require_self// *= require_tree . 

Cracking into AJAX

AJAX: Step 1

Add

:remote => true

to

link_to 'Destroy', station, confirm: 'Are you sure?', :method => :delete

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

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();

Orphans!

It's nearly Christmas, we should help...

Orphans!

BY DESTROYING THEM.

Orphans!

In station.rb:

has_many :streams, :dependent => :destroy

Your Own View Helpers

A Globally Recognized Avatar

Helper Files

These can be found in

“firstfm/app/helpers”

Are included depending on controller.

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

Helper: Now use it in your view!

Task! Using

<%= gravatar_url_for(email) %>

Display the gravatar for the currently logged in user!

top related