SlideShare ist ein Scribd-Unternehmen logo
1 von 37
Downloaden Sie, um offline zu lesen
Topics in Rails:
    Frontin’, p.1
 OR: “You’re Working Too Hard”
         Ben Vandgrift
ben@vandgrift.com / @bvandgrift
   http://ben.vandgrift.com

 http://bit.ly/bv-rails-front
Survey Topics
- Rendering
- Form Helpers and Gems (Formtastic,
  Active Admin)
- HAML, SCSS, and CoffeeScript
- Asset Pipeline
- Caching
The Broad Strokes
- browser makes request
- route to controller
- controller begins processing
 - render :view
- controller finishes processing
- response returned to browser
Basic Rendering
-   render :nothing => true

-   render @something(s)

-   render :action_name # in this controller

-   render ‘path/to/template_name’ # in view path

-   render :inline => “<%= ‘wut’ %>” # booooo

-   render :json => @object

-   render :xml => @object

-   render :js => “alert(‘WUT’);” # booooo
Options? Plenty.
-   :layout => ‘layout_filename’ | false

-   :status => 500 | :forbidden

-   :xml => @photo, :location => photo_url(@photo)

-   :action => ‘edit’

-   ‘books/edit’ | :template => ‘books/edit(.suffix)*’

-   :layout => ‘layout_filename’ | false

-   :file => ‘/path/to/some/template.haml’,
      :content-type => ‘application/donuts’

-   mix and match, sort of.
But Really...
- render does a lot of the work for you
- render @object # finds the partial for you
- render @objects # makes good decisions
- remember @object.to_s and @object.to_param
- stop working so hard
 - write good partials, and life is easier
 - let rails do the work
Honorable Mentions
- head :bad_request # etc

  - most HTTP headers represented

  - head :301/2, :location => ‘/somewhere’

- redirect_to :action => :explode

  - options identical to link_to and url_for

  - can include :status => :whatever

  - :back # does exactly what you expect
Layouts
- a layout is a template with one
  or more yield that lives in a
  special house. THAT’S ALL!
- layouts are inherited unless
  respecified
- layout “name”/false
 - :except/:only => [:action]
-   layout.haml
    !!!html
    %head
      = yield :header
    %body
      #content
        = yield
      #sidebar
        - if content_for? :sidebar
          = yield :sidebar
        - else
          = render ‘sidebar’

-   view.haml
    - content_for :header do
      %title= @kitteh
      = stylesheet_link_tag “extra_stuff”
    - end

    %h1= @kitteh # calls .to_s, make life easy
    = render @kitteh.toys
Brief Q/A?
Forms
- form_tag #in case of emergency, otherwise:
  form_for @object, {:url => {}, :html =>
  {}} do |f|
    f.field_type :attribute_name
  end
- :remote => true # yes, please (another
  show?)
- f.fields_for :associated_model do |am|
- extra credit: CSRF and authenticity tokens
- but that’s all working too hard.
gem ‘formtastic’
-   semantic_form_for @kitten do |f|
      f.inputs :breed, :color, :is_bitchy?
      f.actions :submit, :cancel
    end #DONE

-   all those :symbols? unnecessary!

-   options a-plenty!

-   (!) not so awesome with namespace’d and nested
    forms #meh

-   still working too hard
gem ‘active_admin’
- does much of the heavy lifting
  for you
- very configurable
- uses formtastic notation
- looks vurry vurry niiice
- http://activeadmin.info/
gem ‘haml’ # > erb
-   <div id="profile">
      <div class="left column">
        <div id="date"><%= print_date %></div>
        <div id="address"><%= current_user.address %></div>
      </div>
      <div class="right column">
        <div id="email"><%= current_user.email %></div>
        <div id="bio"><%= current_user.bio %></div>
      </div>
    </div> <!-- ugly erb -->

-   #profile
      .left.column
        #date= print_date
        #address= current_user.address
      .right.column
        #email= current_user.email
        #bio= current_user.bio
haml syntax

- indentation => block structure
- %tag{:attribute => “value”}
- .div-class-name
- #div-id
haml interpolation
- = some_method_with_output(args)

- - some_method_no_output(args)

- filters!

  - :javascript
      alert(‘yuck!’);

  - :erb
      <%= “don’t make me come over there.” %>

  - :coffeescript (w/gem ‘coffee-filter’)
      alert ‘nice segue, eh?’
coffeescript
- .js:
  square = function(x) {
    return x * x;
  };
  if (typeof elvis !== “undefined” &&
  elvis !== null) {
    alert(“I knew it!”);
  }
- .coffee:
  square = (x) -> x * x
  alert “I knew it!” if elvis
coffeescript, ctd.
-   fewer {}, no ; or var, function() simplified

-   lexical scope handled by block

-   better truthiness

-   rb-like returns

-   easier to read and define loops

-   inline decisions and existential op

    -   alert “whut” if errors?

-   gem ‘coffee-rails’, included by default.
SASS / .scss
- what happened to .sass?
-   @import “colors” // list of color variables
    .sidebar {
      a {
        color: $link;
        text-decoration: none;
        &:hover {
          color: $accent;
        }
      }

-   nesting, variables, functions, imports, mixins and more.

-   visit the Charlotte UX group!
Brief Q/A?
Where does all this
       live?
In the asshat asset pipeline, of course.
Views and Assets
- app/views/* #=> views
- app/views/layouts/* #=> layouts
- assets/* #=> js, css, images
- vendor/assets/*
- lib/assets/*
- public/* #=> just files
Asset Pipeline
- if it ain’t broke? wut?
- .scss/.js/.coffee easier to
  find, maintain, and debug
- isolates vendor/lib stuff
- minifies/packages everything for
  faster page loads
- fingerprints for cachiness
Asset Pipeline, ctd
- Manifests: application.(js|css)
- .js:
 - //= require thing
   //= require_tree ../templates

- .css:
 - *= require_self
   *= require path/to/thing
Asset Pipeline, ctd
- use the helpers!
 - javascript_include_tag
 - stylesheet_include_tag
 - ..and magic friends
- update your web server config in
  production, btw.
Search Paths?
- In config/application.rb or
  config/environments/whatever.rb
 - config.assets.paths << rrj(..)
- In controllers/initializers:
 - prepend_view_path(path)
 - NOT a great implementation,
   ensure this only happens once
MOAR extra credit

- config.asset_host #cdns!
- config.asset_path #weird deploys
- config/locales/en.yml
 - %h1=t :awesome_title
Brief Q/A?
Caching in General
- Caching prevents unnecessary
  execution and network traffic.
 - Browser Caches (Etags?)
 - Network Caching (CDN?)
 - Query Caching (Another Show?)
- Steve Souders:
 - http://www.stevesouders.com/blog/
 - High Performance Web Sites
- Make Fewer HTTP      - Make JavaScript
  Requests               and CSS External
- Use a Content        - Reduce DNS Lookups
  Delivery Network
                       - Minify JavaScript
- Add an Expires
  Header               - Avoid Redirects

- Gzip Components      - Remove Duplicate
                         Scripts
- Put Stylesheets at
  the Top              - Configure ETags

- Put Scripts at the   - Make AJAX
  Bottom                 Cacheable

- Avoid CSS            - ... more details
  Expressions            at Souders’ blog
Caching in Rails
- Page Caching
- Action Caching
- Fragment/Partial Caching
- Sweepers
- don’t forget to turn it on!
 -   config.action_controller.perform_caching = true
Page Caching
- captures the whole page as
  an .html file in public/
- adjust your web server
  accordingly
- In the controller:
 - caches_page :action_name
 - expire_page :action => :action_name
Action Caching
- Use when filters are important!
- config.cache_store = :store_type, {}
- :memory_store, :file_store,
  :mem_cache_store, :ehcache (jRuby)
- caches_action / expire_action
- options same as page caching. mostly.
Fragment Caching
‣   same as Action cache, but happens granularly in the view

    - cache(:action => :something,
            :action_suffix => “kitteh_toys”) do
        %h1= @kitteh
        = render @kitteh.toys
        ...
    - end

    expire_fragment(options + :controller => :something)

    or use a string id:
    - cache(“stuff!”) do
Sweepers

- like Observer
- @object gets passed to sweeper
- have callbacks to @object’s
  lifecycle events
- better than managing it yourself
One Last Q/A?
Thanks!
ben@vandgrift.com / @bvandgrift
   http://ben.vandgrift.com

Weitere ähnliche Inhalte

Was ist angesagt?

Cheap frontend tricks
Cheap frontend tricksCheap frontend tricks
Cheap frontend tricksambiescent
 
Game Development Using HTML 5
Game Development Using HTML 5Game Development Using HTML 5
Game Development Using HTML 5osa_ora
 
Ionic tabs template explained
Ionic tabs template explainedIonic tabs template explained
Ionic tabs template explainedRamesh BN
 
HTML5: where flash isn't needed anymore
HTML5: where flash isn't needed anymoreHTML5: where flash isn't needed anymore
HTML5: where flash isn't needed anymoreRemy Sharp
 
Enjoy the vue.js
Enjoy the vue.jsEnjoy the vue.js
Enjoy the vue.jsTechExeter
 
jQtouch, Building Awesome Webapps
jQtouch, Building Awesome WebappsjQtouch, Building Awesome Webapps
jQtouch, Building Awesome WebappsHome
 
You're Doing it Wrong - WordCamp Atlanta
You're Doing it Wrong - WordCamp AtlantaYou're Doing it Wrong - WordCamp Atlanta
You're Doing it Wrong - WordCamp AtlantaChris Scott
 
Monitoring web application behaviour with cucumber-nagios
Monitoring web application behaviour with cucumber-nagiosMonitoring web application behaviour with cucumber-nagios
Monitoring web application behaviour with cucumber-nagiosLindsay Holmwood
 
Five Pound App talk: hereit.is, Web app architecture, REST, CSS3
Five Pound App talk: hereit.is, Web app architecture, REST, CSS3Five Pound App talk: hereit.is, Web app architecture, REST, CSS3
Five Pound App talk: hereit.is, Web app architecture, REST, CSS3Jamie Matthews
 
Simple Web Apps With Sinatra
Simple Web Apps With SinatraSimple Web Apps With Sinatra
Simple Web Apps With Sinatraa_l
 
Javascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & TricksJavascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & TricksHjörtur Hilmarsson
 
Clearance: Simple, complete Ruby web app authentication.
Clearance: Simple, complete Ruby web app authentication.Clearance: Simple, complete Ruby web app authentication.
Clearance: Simple, complete Ruby web app authentication.Jason Morrison
 
WordPress as the Backbone(.js)
WordPress as the Backbone(.js)WordPress as the Backbone(.js)
WordPress as the Backbone(.js)Beau Lebens
 
Developing for Mobile
Developing for MobileDeveloping for Mobile
Developing for MobileRemy Sharp
 
Really Rapid Admin Application Development
Really Rapid Admin Application DevelopmentReally Rapid Admin Application Development
Really Rapid Admin Application DevelopmentJose Diaz-Gonzalez
 
Dethroning Grunt: Simple and Effective Builds with gulp.js
Dethroning Grunt: Simple and Effective Builds with gulp.jsDethroning Grunt: Simple and Effective Builds with gulp.js
Dethroning Grunt: Simple and Effective Builds with gulp.jsJay Harris
 
Short lived immutable servers with masterless puppet
Short lived immutable servers with masterless puppetShort lived immutable servers with masterless puppet
Short lived immutable servers with masterless puppetNeil Millard
 
Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)
Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)
Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)Nicholas Zakas
 

Was ist angesagt? (20)

Cheap frontend tricks
Cheap frontend tricksCheap frontend tricks
Cheap frontend tricks
 
Game Development Using HTML 5
Game Development Using HTML 5Game Development Using HTML 5
Game Development Using HTML 5
 
Ionic tabs template explained
Ionic tabs template explainedIonic tabs template explained
Ionic tabs template explained
 
HTML5: where flash isn't needed anymore
HTML5: where flash isn't needed anymoreHTML5: where flash isn't needed anymore
HTML5: where flash isn't needed anymore
 
Enjoy the vue.js
Enjoy the vue.jsEnjoy the vue.js
Enjoy the vue.js
 
Brunch With Coffee
Brunch With CoffeeBrunch With Coffee
Brunch With Coffee
 
Killer page load performance
Killer page load performanceKiller page load performance
Killer page load performance
 
jQtouch, Building Awesome Webapps
jQtouch, Building Awesome WebappsjQtouch, Building Awesome Webapps
jQtouch, Building Awesome Webapps
 
You're Doing it Wrong - WordCamp Atlanta
You're Doing it Wrong - WordCamp AtlantaYou're Doing it Wrong - WordCamp Atlanta
You're Doing it Wrong - WordCamp Atlanta
 
Monitoring web application behaviour with cucumber-nagios
Monitoring web application behaviour with cucumber-nagiosMonitoring web application behaviour with cucumber-nagios
Monitoring web application behaviour with cucumber-nagios
 
Five Pound App talk: hereit.is, Web app architecture, REST, CSS3
Five Pound App talk: hereit.is, Web app architecture, REST, CSS3Five Pound App talk: hereit.is, Web app architecture, REST, CSS3
Five Pound App talk: hereit.is, Web app architecture, REST, CSS3
 
Simple Web Apps With Sinatra
Simple Web Apps With SinatraSimple Web Apps With Sinatra
Simple Web Apps With Sinatra
 
Javascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & TricksJavascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & Tricks
 
Clearance: Simple, complete Ruby web app authentication.
Clearance: Simple, complete Ruby web app authentication.Clearance: Simple, complete Ruby web app authentication.
Clearance: Simple, complete Ruby web app authentication.
 
WordPress as the Backbone(.js)
WordPress as the Backbone(.js)WordPress as the Backbone(.js)
WordPress as the Backbone(.js)
 
Developing for Mobile
Developing for MobileDeveloping for Mobile
Developing for Mobile
 
Really Rapid Admin Application Development
Really Rapid Admin Application DevelopmentReally Rapid Admin Application Development
Really Rapid Admin Application Development
 
Dethroning Grunt: Simple and Effective Builds with gulp.js
Dethroning Grunt: Simple and Effective Builds with gulp.jsDethroning Grunt: Simple and Effective Builds with gulp.js
Dethroning Grunt: Simple and Effective Builds with gulp.js
 
Short lived immutable servers with masterless puppet
Short lived immutable servers with masterless puppetShort lived immutable servers with masterless puppet
Short lived immutable servers with masterless puppet
 
Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)
Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)
Progressive Enhancement 2.0 (jQuery Conference SF Bay Area 2011)
 

Andere mochten auch

Kilmacud crokes presentation
Kilmacud crokes presentationKilmacud crokes presentation
Kilmacud crokes presentationemmetryan
 
Market Monitor December 2013
Market Monitor December 2013Market Monitor December 2013
Market Monitor December 2013Yves Van Hee
 
from glider to gulf stream (v.1, BlendConf 2013, Charlotte NC)
from glider to gulf stream (v.1, BlendConf 2013, Charlotte NC)from glider to gulf stream (v.1, BlendConf 2013, Charlotte NC)
from glider to gulf stream (v.1, BlendConf 2013, Charlotte NC)Benjamin Vandgrift
 
Αστρονομία :: Mάθημα 3ο :: Ο Ήλιος (Α' Μέρος)
Αστρονομία :: Mάθημα 3ο :: Ο Ήλιος (Α' Μέρος)Αστρονομία :: Mάθημα 3ο :: Ο Ήλιος (Α' Μέρος)
Αστρονομία :: Mάθημα 3ο :: Ο Ήλιος (Α' Μέρος)3rd High School of Serres
 
Basic tester audio presentation
Basic tester audio presentationBasic tester audio presentation
Basic tester audio presentationdwilsonwested
 

Andere mochten auch (7)

Kilmacud crokes presentation
Kilmacud crokes presentationKilmacud crokes presentation
Kilmacud crokes presentation
 
Market Monitor December 2013
Market Monitor December 2013Market Monitor December 2013
Market Monitor December 2013
 
from glider to gulf stream (v.1, BlendConf 2013, Charlotte NC)
from glider to gulf stream (v.1, BlendConf 2013, Charlotte NC)from glider to gulf stream (v.1, BlendConf 2013, Charlotte NC)
from glider to gulf stream (v.1, BlendConf 2013, Charlotte NC)
 
Αστρονομία :: Μάθημα 1ο
Αστρονομία :: Μάθημα 1οΑστρονομία :: Μάθημα 1ο
Αστρονομία :: Μάθημα 1ο
 
Tiny frogs
Tiny frogsTiny frogs
Tiny frogs
 
Αστρονομία :: Mάθημα 3ο :: Ο Ήλιος (Α' Μέρος)
Αστρονομία :: Mάθημα 3ο :: Ο Ήλιος (Α' Μέρος)Αστρονομία :: Mάθημα 3ο :: Ο Ήλιος (Α' Μέρος)
Αστρονομία :: Mάθημα 3ο :: Ο Ήλιος (Α' Μέρος)
 
Basic tester audio presentation
Basic tester audio presentationBasic tester audio presentation
Basic tester audio presentation
 

Ähnlich wie Survey of Front End Topics in Rails

Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overviewYehuda Katz
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the FinishYehuda Katz
 
JavaServer Faces 2.0 - JavaOne India 2011
JavaServer Faces 2.0 - JavaOne India 2011JavaServer Faces 2.0 - JavaOne India 2011
JavaServer Faces 2.0 - JavaOne India 2011Arun Gupta
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVCAlive Kuo
 
Rails + Sencha = Netzke
Rails + Sencha = NetzkeRails + Sencha = Netzke
Rails + Sencha = Netzkebeffa
 
Styling components with JavaScript
Styling components with JavaScriptStyling components with JavaScript
Styling components with JavaScriptbensmithett
 
以Vue開發電子商務網站
架構與眉角
以Vue開發電子商務網站
架構與眉角以Vue開發電子商務網站
架構與眉角
以Vue開發電子商務網站
架構與眉角Mei-yu Chen
 
Everything is Awesome - Cutting the Corners off the Web
Everything is Awesome - Cutting the Corners off the WebEverything is Awesome - Cutting the Corners off the Web
Everything is Awesome - Cutting the Corners off the WebJames Rakich
 
AngularJS vs. Ember.js vs. Backbone.js
AngularJS vs. Ember.js vs. Backbone.jsAngularJS vs. Ember.js vs. Backbone.js
AngularJS vs. Ember.js vs. Backbone.jsMark
 
Angular.js Fundamentals
Angular.js FundamentalsAngular.js Fundamentals
Angular.js FundamentalsMark
 
Node.js & Twitter Bootstrap Crash Course
Node.js & Twitter Bootstrap Crash CourseNode.js & Twitter Bootstrap Crash Course
Node.js & Twitter Bootstrap Crash CourseAaron Silverman
 
Web applications with Catalyst
Web applications with CatalystWeb applications with Catalyst
Web applications with Catalystsvilen.ivanov
 
Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Chris Alfano
 
How to replace rails asset pipeline with webpack?
How to replace rails asset pipeline with webpack?How to replace rails asset pipeline with webpack?
How to replace rails asset pipeline with webpack?Tomasz Bak
 

Ähnlich wie Survey of Front End Topics in Rails (20)

Death of a Themer
Death of a ThemerDeath of a Themer
Death of a Themer
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 
Play vs Rails
Play vs RailsPlay vs Rails
Play vs Rails
 
JavaServer Faces 2.0 - JavaOne India 2011
JavaServer Faces 2.0 - JavaOne India 2011JavaServer Faces 2.0 - JavaOne India 2011
JavaServer Faces 2.0 - JavaOne India 2011
 
The Rails Way
The Rails WayThe Rails Way
The Rails Way
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC
 
Web-Performance
Web-PerformanceWeb-Performance
Web-Performance
 
Please dont touch-3.6-jsday
Please dont touch-3.6-jsdayPlease dont touch-3.6-jsday
Please dont touch-3.6-jsday
 
Rails + Sencha = Netzke
Rails + Sencha = NetzkeRails + Sencha = Netzke
Rails + Sencha = Netzke
 
Styling components with JavaScript
Styling components with JavaScriptStyling components with JavaScript
Styling components with JavaScript
 
以Vue開發電子商務網站
架構與眉角
以Vue開發電子商務網站
架構與眉角以Vue開發電子商務網站
架構與眉角
以Vue開發電子商務網站
架構與眉角
 
Everything is Awesome - Cutting the Corners off the Web
Everything is Awesome - Cutting the Corners off the WebEverything is Awesome - Cutting the Corners off the Web
Everything is Awesome - Cutting the Corners off the Web
 
AngularJS vs. Ember.js vs. Backbone.js
AngularJS vs. Ember.js vs. Backbone.jsAngularJS vs. Ember.js vs. Backbone.js
AngularJS vs. Ember.js vs. Backbone.js
 
Angular.js Fundamentals
Angular.js FundamentalsAngular.js Fundamentals
Angular.js Fundamentals
 
Node.js & Twitter Bootstrap Crash Course
Node.js & Twitter Bootstrap Crash CourseNode.js & Twitter Bootstrap Crash Course
Node.js & Twitter Bootstrap Crash Course
 
Web applications with Catalyst
Web applications with CatalystWeb applications with Catalyst
Web applications with Catalyst
 
Mojolicious
MojoliciousMojolicious
Mojolicious
 
Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011
 
How to replace rails asset pipeline with webpack?
How to replace rails asset pipeline with webpack?How to replace rails asset pipeline with webpack?
How to replace rails asset pipeline with webpack?
 

Kürzlich hochgeladen

Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

Survey of Front End Topics in Rails

  • 1. Topics in Rails: Frontin’, p.1 OR: “You’re Working Too Hard” Ben Vandgrift ben@vandgrift.com / @bvandgrift http://ben.vandgrift.com http://bit.ly/bv-rails-front
  • 2. Survey Topics - Rendering - Form Helpers and Gems (Formtastic, Active Admin) - HAML, SCSS, and CoffeeScript - Asset Pipeline - Caching
  • 3. The Broad Strokes - browser makes request - route to controller - controller begins processing - render :view - controller finishes processing - response returned to browser
  • 4. Basic Rendering - render :nothing => true - render @something(s) - render :action_name # in this controller - render ‘path/to/template_name’ # in view path - render :inline => “<%= ‘wut’ %>” # booooo - render :json => @object - render :xml => @object - render :js => “alert(‘WUT’);” # booooo
  • 5. Options? Plenty. - :layout => ‘layout_filename’ | false - :status => 500 | :forbidden - :xml => @photo, :location => photo_url(@photo) - :action => ‘edit’ - ‘books/edit’ | :template => ‘books/edit(.suffix)*’ - :layout => ‘layout_filename’ | false - :file => ‘/path/to/some/template.haml’, :content-type => ‘application/donuts’ - mix and match, sort of.
  • 6. But Really... - render does a lot of the work for you - render @object # finds the partial for you - render @objects # makes good decisions - remember @object.to_s and @object.to_param - stop working so hard - write good partials, and life is easier - let rails do the work
  • 7. Honorable Mentions - head :bad_request # etc - most HTTP headers represented - head :301/2, :location => ‘/somewhere’ - redirect_to :action => :explode - options identical to link_to and url_for - can include :status => :whatever - :back # does exactly what you expect
  • 8. Layouts - a layout is a template with one or more yield that lives in a special house. THAT’S ALL! - layouts are inherited unless respecified - layout “name”/false - :except/:only => [:action]
  • 9. - layout.haml !!!html %head = yield :header %body #content = yield #sidebar - if content_for? :sidebar = yield :sidebar - else = render ‘sidebar’ - view.haml - content_for :header do %title= @kitteh = stylesheet_link_tag “extra_stuff” - end %h1= @kitteh # calls .to_s, make life easy = render @kitteh.toys
  • 11. Forms - form_tag #in case of emergency, otherwise: form_for @object, {:url => {}, :html => {}} do |f| f.field_type :attribute_name end - :remote => true # yes, please (another show?) - f.fields_for :associated_model do |am| - extra credit: CSRF and authenticity tokens - but that’s all working too hard.
  • 12. gem ‘formtastic’ - semantic_form_for @kitten do |f| f.inputs :breed, :color, :is_bitchy? f.actions :submit, :cancel end #DONE - all those :symbols? unnecessary! - options a-plenty! - (!) not so awesome with namespace’d and nested forms #meh - still working too hard
  • 13. gem ‘active_admin’ - does much of the heavy lifting for you - very configurable - uses formtastic notation - looks vurry vurry niiice - http://activeadmin.info/
  • 14. gem ‘haml’ # > erb - <div id="profile"> <div class="left column"> <div id="date"><%= print_date %></div> <div id="address"><%= current_user.address %></div> </div> <div class="right column"> <div id="email"><%= current_user.email %></div> <div id="bio"><%= current_user.bio %></div> </div> </div> <!-- ugly erb --> - #profile .left.column #date= print_date #address= current_user.address .right.column #email= current_user.email #bio= current_user.bio
  • 15. haml syntax - indentation => block structure - %tag{:attribute => “value”} - .div-class-name - #div-id
  • 16. haml interpolation - = some_method_with_output(args) - - some_method_no_output(args) - filters! - :javascript alert(‘yuck!’); - :erb <%= “don’t make me come over there.” %> - :coffeescript (w/gem ‘coffee-filter’) alert ‘nice segue, eh?’
  • 17. coffeescript - .js: square = function(x) { return x * x; }; if (typeof elvis !== “undefined” && elvis !== null) { alert(“I knew it!”); } - .coffee: square = (x) -> x * x alert “I knew it!” if elvis
  • 18. coffeescript, ctd. - fewer {}, no ; or var, function() simplified - lexical scope handled by block - better truthiness - rb-like returns - easier to read and define loops - inline decisions and existential op - alert “whut” if errors? - gem ‘coffee-rails’, included by default.
  • 19. SASS / .scss - what happened to .sass? - @import “colors” // list of color variables .sidebar { a { color: $link; text-decoration: none; &:hover { color: $accent; } } - nesting, variables, functions, imports, mixins and more. - visit the Charlotte UX group!
  • 21. Where does all this live? In the asshat asset pipeline, of course.
  • 22. Views and Assets - app/views/* #=> views - app/views/layouts/* #=> layouts - assets/* #=> js, css, images - vendor/assets/* - lib/assets/* - public/* #=> just files
  • 23. Asset Pipeline - if it ain’t broke? wut? - .scss/.js/.coffee easier to find, maintain, and debug - isolates vendor/lib stuff - minifies/packages everything for faster page loads - fingerprints for cachiness
  • 24. Asset Pipeline, ctd - Manifests: application.(js|css) - .js: - //= require thing //= require_tree ../templates - .css: - *= require_self *= require path/to/thing
  • 25. Asset Pipeline, ctd - use the helpers! - javascript_include_tag - stylesheet_include_tag - ..and magic friends - update your web server config in production, btw.
  • 26. Search Paths? - In config/application.rb or config/environments/whatever.rb - config.assets.paths << rrj(..) - In controllers/initializers: - prepend_view_path(path) - NOT a great implementation, ensure this only happens once
  • 27. MOAR extra credit - config.asset_host #cdns! - config.asset_path #weird deploys - config/locales/en.yml - %h1=t :awesome_title
  • 29. Caching in General - Caching prevents unnecessary execution and network traffic. - Browser Caches (Etags?) - Network Caching (CDN?) - Query Caching (Another Show?) - Steve Souders: - http://www.stevesouders.com/blog/ - High Performance Web Sites
  • 30. - Make Fewer HTTP - Make JavaScript Requests and CSS External - Use a Content - Reduce DNS Lookups Delivery Network - Minify JavaScript - Add an Expires Header - Avoid Redirects - Gzip Components - Remove Duplicate Scripts - Put Stylesheets at the Top - Configure ETags - Put Scripts at the - Make AJAX Bottom Cacheable - Avoid CSS - ... more details Expressions at Souders’ blog
  • 31. Caching in Rails - Page Caching - Action Caching - Fragment/Partial Caching - Sweepers - don’t forget to turn it on! - config.action_controller.perform_caching = true
  • 32. Page Caching - captures the whole page as an .html file in public/ - adjust your web server accordingly - In the controller: - caches_page :action_name - expire_page :action => :action_name
  • 33. Action Caching - Use when filters are important! - config.cache_store = :store_type, {} - :memory_store, :file_store, :mem_cache_store, :ehcache (jRuby) - caches_action / expire_action - options same as page caching. mostly.
  • 34. Fragment Caching ‣ same as Action cache, but happens granularly in the view - cache(:action => :something, :action_suffix => “kitteh_toys”) do %h1= @kitteh = render @kitteh.toys ... - end expire_fragment(options + :controller => :something) or use a string id: - cache(“stuff!”) do
  • 35. Sweepers - like Observer - @object gets passed to sweeper - have callbacks to @object’s lifecycle events - better than managing it yourself
  • 37. Thanks! ben@vandgrift.com / @bvandgrift http://ben.vandgrift.com