SlideShare una empresa de Scribd logo
1 de 29
Descargar para leer sin conexión
Rangular development
Using angular with Rails
(for Single Page web apps)
Jamie Davidson
Pathgather // follow me : @jamiehdavidson
Thursday, June 20, 13
Traditional Rails request flow
Pathgather // follow me : @jamiehdavidson
/users/3
navigates to
✓ Is route defined in routes.rb?
resources :users
- Ensure it’s an HTML request
- Run before_filters, authenticate user
- fetch the user, store in instance variable
application.html.erb/.haml
show.html.erb/.haml
<h1><%= @user.name %></h1>
Thursday, June 20, 13
With Angular + Rails,
this workflow is
usually bypassed....but
not always
Thursday, June 20, 13
Step 1: Build an API
‘Shell’ controllers to handle html requests
Often bypassed
Pathgather // follow me : @jamiehdavidson
api controllers to handle json requests
Never Bypassed
Thursday, June 20, 13
+Rails Code Structure
Startup Institute boston // follow me : @jamiehdavidson
Few Html requests = Few to
none rails helpers
Thursday, June 20, 13
+API vs HTMl Controller
Pathgather // follow me : @jamiehdavidson
app/controllers/users_controller.rb app/controllers/api/v1/users_controller.rb
* Only exists to accept full HTML requests to /users/:id.
Often bypassed
* Separate base controller allows us to specify before_filters
and handle errors specific to API requests
Thursday, June 20, 13
Step 2: Define JSON View
‘Shell’ html views to handle html requests
Often bypassed
Pathgather // follow me : @jamiehdavidson
json views to define returned json structure
Never bypassed
Thursday, June 20, 13
+ RABL
Pathgather // follow me : @jamiehdavidson
app/views/users/show.html.haml app/views/users/show.json.rabl
* Totally blank file. Seriously, it exists, but contains
nothing.
Maybe there’s a better way???
https://github.com/nesquena/rabl
http://railscasts.com/episodes/322-rabl
Railscast:
Thursday, June 20, 13
Step 3: Add angular
why angular over the others?
Pathgather // follow me : @jamiehdavidson
how does it change our normal rails-y javscript stucture?
Thursday, June 20, 13
+Angular Code Structure
No more users.js.coffee,
products.js.coffee, etc, with random
jquery selectors
Extra resources:
- http://briantford.com/blog/
huuuuuge-angular-apps.html
- http://cliffmeyers.com/blog/
2013/4/21/code-organization-
angularjs-javascript
Thursday, June 20, 13
+ Angular App Specification
Restangular over $http or ng-
resource (https://github.com/
mgonto/restangular)
jst covered in a few
Thursday, June 20, 13
Step 3A: Angular Controllers
sends requests to your rails api,
saves response to a scope variable
Pathgather // follow me : @jamiehdavidson
Don’t touch the dom!!!
Don’t even query the dom
Creates listeners for changes made by user
$watch is your friend
Thursday, June 20, 13
+ Angular Controller + View
app/assets/javascripts/my_app/controllers/user.js.coffee app/assets/javascripts/templates/users/show.hamlc
Note: I don’t like this. USer sees a blank template before
seeing the actual user data
Instead, query for your data before the template
loads. leads to a smoother transition between views:
Thursday, June 20, 13
+Handling HTTP ERRORS
No ugly red error pages by default anymore
Need to ‘intercept’ api errors and render an error
page
reuse those ugly red pages
What happens when the user makes an api request for data that doesn’t exist or data they
aren’t allowed to view?
yourapp.js.coffee
Thursday, June 20, 13
pathgather // follow me : @jamiehdavidson
HTML
https://github.com/netzpirat/haml_coffee_assets
Haml can be used for angular templates
closing html tags
Thursday, June 20, 13
Angular SERVICES
SINGLETONS...VERY IMPORTANT!!
Pathgather // follow me : @jamiehdavidson
move global javascript functions/helpers to services
Can be used as ‘wrappers’ to other services/data
Thursday, June 20, 13
+ Service example: current user
let’s see some real code
Thursday, June 20, 13
Angular Filters
Simply used to format data
Pathgather // follow me : @jamiehdavidson
if you need to format data by rendering/manipulating
html, use a directive
Angular ships with many predefined filters:
- formatting dates and currency
- searching within an array (see angular docs)
- http://www.cheatography.com/proloser/cheat-sheets/angularjs/
Thursday, June 20, 13
Angular Directives
horribly complicated
Pathgather // follow me : @jamiehdavidson
Highly technical documentation
But read, re-read, and re-re-read
absurdly powerful
Register behavior, transform the DOM, create reusable widgets, etc
Unfortunately, i could spend this entire talk on
directives, and you’d probably just leave confused
Angular ships with many out of the box
ng-repeat, ng-cloak, ng-view, ng-*
(Create your own prefix. Don’t use ng)
Thursday, June 20, 13
+Directives and jquery plugins
DON’t:
Text
/app/assets/javascripts/[some model].js.coffee
Minimal:
Thursday, June 20, 13
A note about jquery and angular
I find myself very hesitant to use jquery only plugins
Pathgather // follow me : @jamiehdavidson
when starting out with angular, don’t include jquery
‘Roll your own’ first, jquery plugin last
share your directives on github. sharing is caring!!
set a maximum number (single digits) of jquery-only
plugins you’ll use
Thursday, June 20, 13
+User Authentication/Authorization
Server side authentication doesn’t change (beyond
API authentication), but you can’t do this anymore!!
So how do we alter the view to dynamically
display html based on who the current user is?
Thursday, June 20, 13
+Angular + Rails user authentication
You can send AJAX requests, do full page loads, or....
Current user service + directives
But Wait, this is javascript. won’t some average hacker be able to
disguise themselves as another user?
Yep. But we’re good little rails developers, so we have server-side checks (devise, cancan, etc) to ensure the
logged-in user can actually access/modify the data.
they can make themselves look like the current user On the client...but they can’t act like them
Thursday, June 20, 13
+ Angular + SEO
What a crawler sees What our user sees
Thursday, June 20, 13
+ Angular + SEO
Manual lifting required
You have to build a pre-rendered version and render that
to the crawler only
Stay Tuned for crawler improvements and js-heavy/one
page apps
Resources:
- http://www.yearofmoo.com/2012/11/angularjs-and-
seo.html
- https://developers.google.com/webmasters/ajax-
crawling
Thursday, June 20, 13
Final Resources
Pathgather // follow me : @jamiehdavidson
Angular UI
angular-ui.github.io
angular screencasts
egghead.io
railscast
http://www.cheatography.com/proloser/cheat-sheets/angularjs/
Cheat Sheet
https://github.com/jmcunningham/AngularJS-Learning
Github repo of learning resources
http://railscasts.com/episodes/405-angularjs
Thursday, June 20, 13
Oh, we’re
hiring.
Pathgather // follow me : @jamiehdavidson
Thursday, June 20, 13
THANKS.FOR YOUR ATTENTION
Pathgather // follow me : @jamiehdavidson
Thursday, June 20, 13
Q&Atime for me to shutup and for you to talk
or i can tell you more about directives
Pathgather // follow me : @jamiehdavidson
Thursday, June 20, 13

Más contenido relacionado

La actualidad más candente

jQuery and AJAX with Rails
jQuery and AJAX with RailsjQuery and AJAX with Rails
jQuery and AJAX with RailsAlan Hecht
 
RoR 101: Session 6
RoR 101: Session 6RoR 101: Session 6
RoR 101: Session 6Rory Gianni
 
RoR 101: Session 5
RoR 101: Session 5RoR 101: Session 5
RoR 101: Session 5Rory Gianni
 
RoR 101: Session 6
RoR 101: Session 6RoR 101: Session 6
RoR 101: Session 6Rory Gianni
 
RoR 101: Session 3
RoR 101: Session 3RoR 101: Session 3
RoR 101: Session 3Rory Gianni
 
Be happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP ItuBe happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP ItuLucas Renan
 
Rails 2.3 and Rack - NHRuby Feb 2009
Rails 2.3 and Rack - NHRuby Feb 2009Rails 2.3 and Rack - NHRuby Feb 2009
Rails 2.3 and Rack - NHRuby Feb 2009bturnbull
 
Introduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress DevelopersIntroduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress DevelopersCaldera Labs
 
Streamlining Your Applications with Web Frameworks
Streamlining Your Applications with Web FrameworksStreamlining Your Applications with Web Frameworks
Streamlining Your Applications with Web Frameworksguestf7bc30
 
multiple views and routing
multiple views and routingmultiple views and routing
multiple views and routingBrajesh Yadav
 
Introduction à Ruby
Introduction à RubyIntroduction à Ruby
Introduction à RubyMicrosoft
 
Webinar: AngularJS and the WordPress REST API
Webinar: AngularJS and the WordPress REST APIWebinar: AngularJS and the WordPress REST API
Webinar: AngularJS and the WordPress REST APIWP Engine UK
 
Ride on the Fast Track of Web with Ruby on Rails- Part 2
Ride on the Fast Track of Web with Ruby on Rails- Part 2Ride on the Fast Track of Web with Ruby on Rails- Part 2
Ride on the Fast Track of Web with Ruby on Rails- Part 2A.K.M. Ahsrafuzzaman
 
Session 5 : intro to jsp - Giáo trình Bách Khoa Aptech
Session 5 : intro to jsp  - Giáo trình Bách Khoa AptechSession 5 : intro to jsp  - Giáo trình Bách Khoa Aptech
Session 5 : intro to jsp - Giáo trình Bách Khoa AptechMasterCode.vn
 
Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...
Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...
Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...Caldera Labs
 
Webapps without the web
Webapps without the webWebapps without the web
Webapps without the webRemy Sharp
 
Bullet: The Functional PHP Micro-Framework
Bullet: The Functional PHP Micro-FrameworkBullet: The Functional PHP Micro-Framework
Bullet: The Functional PHP Micro-FrameworkVance Lucas
 
AngularJS with Slim PHP Micro Framework
AngularJS with Slim PHP Micro FrameworkAngularJS with Slim PHP Micro Framework
AngularJS with Slim PHP Micro FrameworkBackand Cohen
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsMike Subelsky
 

La actualidad más candente (20)

jQuery and AJAX with Rails
jQuery and AJAX with RailsjQuery and AJAX with Rails
jQuery and AJAX with Rails
 
RoR 101: Session 6
RoR 101: Session 6RoR 101: Session 6
RoR 101: Session 6
 
RoR 101: Session 5
RoR 101: Session 5RoR 101: Session 5
RoR 101: Session 5
 
RoR 101: Session 6
RoR 101: Session 6RoR 101: Session 6
RoR 101: Session 6
 
RoR 101: Session 3
RoR 101: Session 3RoR 101: Session 3
RoR 101: Session 3
 
Be happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP ItuBe happy with Ruby on Rails - CEUNSP Itu
Be happy with Ruby on Rails - CEUNSP Itu
 
Rails::Engine
Rails::EngineRails::Engine
Rails::Engine
 
Rails 2.3 and Rack - NHRuby Feb 2009
Rails 2.3 and Rack - NHRuby Feb 2009Rails 2.3 and Rack - NHRuby Feb 2009
Rails 2.3 and Rack - NHRuby Feb 2009
 
Introduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress DevelopersIntroduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress Developers
 
Streamlining Your Applications with Web Frameworks
Streamlining Your Applications with Web FrameworksStreamlining Your Applications with Web Frameworks
Streamlining Your Applications with Web Frameworks
 
multiple views and routing
multiple views and routingmultiple views and routing
multiple views and routing
 
Introduction à Ruby
Introduction à RubyIntroduction à Ruby
Introduction à Ruby
 
Webinar: AngularJS and the WordPress REST API
Webinar: AngularJS and the WordPress REST APIWebinar: AngularJS and the WordPress REST API
Webinar: AngularJS and the WordPress REST API
 
Ride on the Fast Track of Web with Ruby on Rails- Part 2
Ride on the Fast Track of Web with Ruby on Rails- Part 2Ride on the Fast Track of Web with Ruby on Rails- Part 2
Ride on the Fast Track of Web with Ruby on Rails- Part 2
 
Session 5 : intro to jsp - Giáo trình Bách Khoa Aptech
Session 5 : intro to jsp  - Giáo trình Bách Khoa AptechSession 5 : intro to jsp  - Giáo trình Bách Khoa Aptech
Session 5 : intro to jsp - Giáo trình Bách Khoa Aptech
 
Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...
Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...
Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...
 
Webapps without the web
Webapps without the webWebapps without the web
Webapps without the web
 
Bullet: The Functional PHP Micro-Framework
Bullet: The Functional PHP Micro-FrameworkBullet: The Functional PHP Micro-Framework
Bullet: The Functional PHP Micro-Framework
 
AngularJS with Slim PHP Micro Framework
AngularJS with Slim PHP Micro FrameworkAngularJS with Slim PHP Micro Framework
AngularJS with Slim PHP Micro Framework
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web Apps
 

Similar a Using Angular with Rails

rendre AJAX crawlable par les moteurs
rendre AJAX crawlable par les moteursrendre AJAX crawlable par les moteurs
rendre AJAX crawlable par les moteursSerge Esteves
 
React seo tips to build seo friendly web applications
React seo tips to build seo friendly web applicationsReact seo tips to build seo friendly web applications
React seo tips to build seo friendly web applicationsKaty Slemon
 
Techniques For A Modern Web UI (With Notes)
Techniques For A Modern Web UI (With Notes)Techniques For A Modern Web UI (With Notes)
Techniques For A Modern Web UI (With Notes)patrick.t.joyce
 
Professional web development with libraries
Professional web development with librariesProfessional web development with libraries
Professional web development with librariesChristian Heilmann
 
Tek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJSTek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJSPablo Godel
 
Understanding progressive enhancement - yuiconf2010
Understanding progressive enhancement - yuiconf2010Understanding progressive enhancement - yuiconf2010
Understanding progressive enhancement - yuiconf2010Christian Heilmann
 
Unlearning and Relearning jQuery - Client-side Performance Optimization
Unlearning and Relearning jQuery - Client-side Performance OptimizationUnlearning and Relearning jQuery - Client-side Performance Optimization
Unlearning and Relearning jQuery - Client-side Performance OptimizationJon Dean
 
JBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
JBUG 11 - Django-The Web Framework For Perfectionists With DeadlinesJBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
JBUG 11 - Django-The Web Framework For Perfectionists With DeadlinesTikal Knowledge
 
More than a side salad: behaviour driven testing and test driven design in Dj...
More than a side salad: behaviour driven testing and test driven design in Dj...More than a side salad: behaviour driven testing and test driven design in Dj...
More than a side salad: behaviour driven testing and test driven design in Dj...Danielle Madeley
 
Single Page Apps - Gerry White @ BrightonSEO
Single Page Apps - Gerry White @ BrightonSEOSingle Page Apps - Gerry White @ BrightonSEO
Single Page Apps - Gerry White @ BrightonSEOGerry White
 
Marionette: Building your first app
Marionette: Building your first appMarionette: Building your first app
Marionette: Building your first appfrontendne
 
SES Toronto 2008; Joe Dolson
SES Toronto 2008; Joe DolsonSES Toronto 2008; Joe Dolson
SES Toronto 2008; Joe DolsonJoseph Dolson
 
Learning from the Best jQuery Plugins
Learning from the Best jQuery PluginsLearning from the Best jQuery Plugins
Learning from the Best jQuery PluginsMarc Grabanski
 
Google I/O 2012 - Protecting your user experience while integrating 3rd party...
Google I/O 2012 - Protecting your user experience while integrating 3rd party...Google I/O 2012 - Protecting your user experience while integrating 3rd party...
Google I/O 2012 - Protecting your user experience while integrating 3rd party...Patrick Meenan
 
Making the Right 2.0 Choice
Making the Right 2.0 ChoiceMaking the Right 2.0 Choice
Making the Right 2.0 Choicemjjames
 
Sexy HTML with Twitter Bootstrap
Sexy HTML with Twitter BootstrapSexy HTML with Twitter Bootstrap
Sexy HTML with Twitter BootstrapKarthik Gaekwad
 
An affiliate's guide - navigating the technical seo minefield - Digital Marke...
An affiliate's guide - navigating the technical seo minefield - Digital Marke...An affiliate's guide - navigating the technical seo minefield - Digital Marke...
An affiliate's guide - navigating the technical seo minefield - Digital Marke...Charlie Williams
 
An Affiliate's Guide - Navigating the Technical SEO Minefield
An Affiliate's Guide - Navigating the Technical SEO MinefieldAn Affiliate's Guide - Navigating the Technical SEO Minefield
An Affiliate's Guide - Navigating the Technical SEO MinefieldDigitalMarketingShow
 

Similar a Using Angular with Rails (20)

rendre AJAX crawlable par les moteurs
rendre AJAX crawlable par les moteursrendre AJAX crawlable par les moteurs
rendre AJAX crawlable par les moteurs
 
React seo tips to build seo friendly web applications
React seo tips to build seo friendly web applicationsReact seo tips to build seo friendly web applications
React seo tips to build seo friendly web applications
 
Techniques For A Modern Web UI (With Notes)
Techniques For A Modern Web UI (With Notes)Techniques For A Modern Web UI (With Notes)
Techniques For A Modern Web UI (With Notes)
 
Professional web development with libraries
Professional web development with librariesProfessional web development with libraries
Professional web development with libraries
 
Tek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJSTek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJS
 
Understanding progressive enhancement - yuiconf2010
Understanding progressive enhancement - yuiconf2010Understanding progressive enhancement - yuiconf2010
Understanding progressive enhancement - yuiconf2010
 
Unlearning and Relearning jQuery - Client-side Performance Optimization
Unlearning and Relearning jQuery - Client-side Performance OptimizationUnlearning and Relearning jQuery - Client-side Performance Optimization
Unlearning and Relearning jQuery - Client-side Performance Optimization
 
JBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
JBUG 11 - Django-The Web Framework For Perfectionists With DeadlinesJBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
JBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
 
Technical Introduction to YDN
Technical Introduction to YDNTechnical Introduction to YDN
Technical Introduction to YDN
 
More than a side salad: behaviour driven testing and test driven design in Dj...
More than a side salad: behaviour driven testing and test driven design in Dj...More than a side salad: behaviour driven testing and test driven design in Dj...
More than a side salad: behaviour driven testing and test driven design in Dj...
 
Single Page Apps - Gerry White @ BrightonSEO
Single Page Apps - Gerry White @ BrightonSEOSingle Page Apps - Gerry White @ BrightonSEO
Single Page Apps - Gerry White @ BrightonSEO
 
Marionette: Building your first app
Marionette: Building your first appMarionette: Building your first app
Marionette: Building your first app
 
SES Toronto 2008; Joe Dolson
SES Toronto 2008; Joe DolsonSES Toronto 2008; Joe Dolson
SES Toronto 2008; Joe Dolson
 
Learning from the Best jQuery Plugins
Learning from the Best jQuery PluginsLearning from the Best jQuery Plugins
Learning from the Best jQuery Plugins
 
Google I/O 2012 - Protecting your user experience while integrating 3rd party...
Google I/O 2012 - Protecting your user experience while integrating 3rd party...Google I/O 2012 - Protecting your user experience while integrating 3rd party...
Google I/O 2012 - Protecting your user experience while integrating 3rd party...
 
How to Make React SEO-friendly
How to  Make React SEO-friendlyHow to  Make React SEO-friendly
How to Make React SEO-friendly
 
Making the Right 2.0 Choice
Making the Right 2.0 ChoiceMaking the Right 2.0 Choice
Making the Right 2.0 Choice
 
Sexy HTML with Twitter Bootstrap
Sexy HTML with Twitter BootstrapSexy HTML with Twitter Bootstrap
Sexy HTML with Twitter Bootstrap
 
An affiliate's guide - navigating the technical seo minefield - Digital Marke...
An affiliate's guide - navigating the technical seo minefield - Digital Marke...An affiliate's guide - navigating the technical seo minefield - Digital Marke...
An affiliate's guide - navigating the technical seo minefield - Digital Marke...
 
An Affiliate's Guide - Navigating the Technical SEO Minefield
An Affiliate's Guide - Navigating the Technical SEO MinefieldAn Affiliate's Guide - Navigating the Technical SEO Minefield
An Affiliate's Guide - Navigating the Technical SEO Minefield
 

Último

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 

Último (20)

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 

Using Angular with Rails

  • 1. Rangular development Using angular with Rails (for Single Page web apps) Jamie Davidson Pathgather // follow me : @jamiehdavidson Thursday, June 20, 13
  • 2. Traditional Rails request flow Pathgather // follow me : @jamiehdavidson /users/3 navigates to ✓ Is route defined in routes.rb? resources :users - Ensure it’s an HTML request - Run before_filters, authenticate user - fetch the user, store in instance variable application.html.erb/.haml show.html.erb/.haml <h1><%= @user.name %></h1> Thursday, June 20, 13
  • 3. With Angular + Rails, this workflow is usually bypassed....but not always Thursday, June 20, 13
  • 4. Step 1: Build an API ‘Shell’ controllers to handle html requests Often bypassed Pathgather // follow me : @jamiehdavidson api controllers to handle json requests Never Bypassed Thursday, June 20, 13
  • 5. +Rails Code Structure Startup Institute boston // follow me : @jamiehdavidson Few Html requests = Few to none rails helpers Thursday, June 20, 13
  • 6. +API vs HTMl Controller Pathgather // follow me : @jamiehdavidson app/controllers/users_controller.rb app/controllers/api/v1/users_controller.rb * Only exists to accept full HTML requests to /users/:id. Often bypassed * Separate base controller allows us to specify before_filters and handle errors specific to API requests Thursday, June 20, 13
  • 7. Step 2: Define JSON View ‘Shell’ html views to handle html requests Often bypassed Pathgather // follow me : @jamiehdavidson json views to define returned json structure Never bypassed Thursday, June 20, 13
  • 8. + RABL Pathgather // follow me : @jamiehdavidson app/views/users/show.html.haml app/views/users/show.json.rabl * Totally blank file. Seriously, it exists, but contains nothing. Maybe there’s a better way??? https://github.com/nesquena/rabl http://railscasts.com/episodes/322-rabl Railscast: Thursday, June 20, 13
  • 9. Step 3: Add angular why angular over the others? Pathgather // follow me : @jamiehdavidson how does it change our normal rails-y javscript stucture? Thursday, June 20, 13
  • 10. +Angular Code Structure No more users.js.coffee, products.js.coffee, etc, with random jquery selectors Extra resources: - http://briantford.com/blog/ huuuuuge-angular-apps.html - http://cliffmeyers.com/blog/ 2013/4/21/code-organization- angularjs-javascript Thursday, June 20, 13
  • 11. + Angular App Specification Restangular over $http or ng- resource (https://github.com/ mgonto/restangular) jst covered in a few Thursday, June 20, 13
  • 12. Step 3A: Angular Controllers sends requests to your rails api, saves response to a scope variable Pathgather // follow me : @jamiehdavidson Don’t touch the dom!!! Don’t even query the dom Creates listeners for changes made by user $watch is your friend Thursday, June 20, 13
  • 13. + Angular Controller + View app/assets/javascripts/my_app/controllers/user.js.coffee app/assets/javascripts/templates/users/show.hamlc Note: I don’t like this. USer sees a blank template before seeing the actual user data Instead, query for your data before the template loads. leads to a smoother transition between views: Thursday, June 20, 13
  • 14. +Handling HTTP ERRORS No ugly red error pages by default anymore Need to ‘intercept’ api errors and render an error page reuse those ugly red pages What happens when the user makes an api request for data that doesn’t exist or data they aren’t allowed to view? yourapp.js.coffee Thursday, June 20, 13
  • 15. pathgather // follow me : @jamiehdavidson HTML https://github.com/netzpirat/haml_coffee_assets Haml can be used for angular templates closing html tags Thursday, June 20, 13
  • 16. Angular SERVICES SINGLETONS...VERY IMPORTANT!! Pathgather // follow me : @jamiehdavidson move global javascript functions/helpers to services Can be used as ‘wrappers’ to other services/data Thursday, June 20, 13
  • 17. + Service example: current user let’s see some real code Thursday, June 20, 13
  • 18. Angular Filters Simply used to format data Pathgather // follow me : @jamiehdavidson if you need to format data by rendering/manipulating html, use a directive Angular ships with many predefined filters: - formatting dates and currency - searching within an array (see angular docs) - http://www.cheatography.com/proloser/cheat-sheets/angularjs/ Thursday, June 20, 13
  • 19. Angular Directives horribly complicated Pathgather // follow me : @jamiehdavidson Highly technical documentation But read, re-read, and re-re-read absurdly powerful Register behavior, transform the DOM, create reusable widgets, etc Unfortunately, i could spend this entire talk on directives, and you’d probably just leave confused Angular ships with many out of the box ng-repeat, ng-cloak, ng-view, ng-* (Create your own prefix. Don’t use ng) Thursday, June 20, 13
  • 20. +Directives and jquery plugins DON’t: Text /app/assets/javascripts/[some model].js.coffee Minimal: Thursday, June 20, 13
  • 21. A note about jquery and angular I find myself very hesitant to use jquery only plugins Pathgather // follow me : @jamiehdavidson when starting out with angular, don’t include jquery ‘Roll your own’ first, jquery plugin last share your directives on github. sharing is caring!! set a maximum number (single digits) of jquery-only plugins you’ll use Thursday, June 20, 13
  • 22. +User Authentication/Authorization Server side authentication doesn’t change (beyond API authentication), but you can’t do this anymore!! So how do we alter the view to dynamically display html based on who the current user is? Thursday, June 20, 13
  • 23. +Angular + Rails user authentication You can send AJAX requests, do full page loads, or.... Current user service + directives But Wait, this is javascript. won’t some average hacker be able to disguise themselves as another user? Yep. But we’re good little rails developers, so we have server-side checks (devise, cancan, etc) to ensure the logged-in user can actually access/modify the data. they can make themselves look like the current user On the client...but they can’t act like them Thursday, June 20, 13
  • 24. + Angular + SEO What a crawler sees What our user sees Thursday, June 20, 13
  • 25. + Angular + SEO Manual lifting required You have to build a pre-rendered version and render that to the crawler only Stay Tuned for crawler improvements and js-heavy/one page apps Resources: - http://www.yearofmoo.com/2012/11/angularjs-and- seo.html - https://developers.google.com/webmasters/ajax- crawling Thursday, June 20, 13
  • 26. Final Resources Pathgather // follow me : @jamiehdavidson Angular UI angular-ui.github.io angular screencasts egghead.io railscast http://www.cheatography.com/proloser/cheat-sheets/angularjs/ Cheat Sheet https://github.com/jmcunningham/AngularJS-Learning Github repo of learning resources http://railscasts.com/episodes/405-angularjs Thursday, June 20, 13
  • 27. Oh, we’re hiring. Pathgather // follow me : @jamiehdavidson Thursday, June 20, 13
  • 28. THANKS.FOR YOUR ATTENTION Pathgather // follow me : @jamiehdavidson Thursday, June 20, 13
  • 29. Q&Atime for me to shutup and for you to talk or i can tell you more about directives Pathgather // follow me : @jamiehdavidson Thursday, June 20, 13