Rails and Javascript

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

to use JS in rails

<%= javascript_include_tag "application" %>

Loading a Manifest File in your layout

<%= stylesheet_link_tag 'application' %>

External API Considerations

Documentation Versioning Authentication and Security Quality of Service

The Asset Pipeline uses a manifest file to tell Rails what to load. This manifest file is a central location where we can list all the CSS and JS files our application needs. This isn't a feature of JS or CSS but rather the asset pipeline

File: app/assets/javascripts/application.js //= require jquery //= require calendar

require directives

For example, if you have the file app/assets/stylesheets/blogs/main.css you will need to require it like this, *= require 'blogs/main'.

POST Params vs POST Body (part 2)

Fortunately, Faraday makes it easy to POST with JSON in the body of the request. Instead of doing: Faraday.post("https://url/to/api") do |req| req.params['my_param'] = my_value end You can do this: Faraday.post("https://url/to/api") do |req| req.body = "{ "my_param": my_value }" end

Given a service that lists sandwiches, what action would we perform to get all of the sandwiches? POST /sandwich GET /allsandwiches GET /sandwiches

GET /sandwiches

Given the same service of sandwiches, what would the action look like if we specifically wanted a BLT? GET /blt GET /sandwiches/blt PUT /blt

GET /sandwiches/blt

CSS Manifest in development vs. production

In development mode, each CSS file will get its own link tag. layout application <link rel="stylesheet" href="/assets/main.css" /> In production mode, Sprockets will take all of our CSS files and create one large CSS file. It will also minify the contents, removing unneeded whitespace to reduce the overall size of the file. Both of these things will speed up page load times for our users <link rel="stylesheet" href="/assets/application-4dd5b109ee3439da54f5bdfd78a80473.css" />

What HTTP verb is typically used to update something? PATCH POST GET DELETE

PATCH

What HTTP verb is typically used to create something new? PUT POST GET DELETE

POST

POST Params vs POST Body (part 1)

The Foursquare API endpoints we've been using all have used parameters to pass data. For a GET request, those are querystring parameters. For a POST, they are www-form-urlencodedparameters - that is, they act as if we had created a form and clicked submit. Other API providers, like Github, may require that POST data instead be JSON-formatted text in the request body. A request has a body just like a response does.

Loading stylesheets into our application can be done a few different ways in Rails.

We can use HTML link tags for CSS located on another server. We can place third party CSS files in our vendor folder. We can also use gems to load the CSS frameworks we need like Twitter Bootstrap.

concatenation

We don't want to serve each file separately as this will make our page load very slow. It makes sense for us to maintain separate small files for readability and organization, but, for the browser, we'd rather smash all those small files together and load 1 JS file and 1 CSS file. This process is called concatenation.

asset concatenation / Asset Pipeline what it does?

combine dozens of JS and CSS files and update the layouts every time we want to make a change and deploy our application

remote true pattern

data-remote = true research it rails only

("<%= j(render (`comments/comment, :comment => @comment)) %>"); what does the j do ?

escaping html so you can embed it into javascript

ActiveModel::Serializer, or AMS process

gem 'active_model_serializers' bundle install rails g serializer post

the choice to build and provide an API boils down to one of two options:

if other companies need your data or if other internal apps need the data

gems rails and JS

jQuery gem in our Gemfile. Once we bundle install, we are able to add //= require jquery to our manifest file. this will auto update our api but could break our code

if you code is depend on loading file in specific order

manually require each file in the order you need it to be loaded and get rid of require tree

CSS Manifests

n a CSS manifest file you must open the CSS comment block with /* and then each directive appears on an individual line starting with *=. You close the comment block with */. It's a little different than the JS directive comments of //=, which are individual, valid JS comments.

spring stop

stop the process of the loaders for gems

to_json method

take our object and, well, convert it to JSON instead of concatenation

why Rails provides the vendor/assets/javascripts folder.

third party JS libraries in here and add them to our JS manifest file

External Stylesheets

what is normal done Or what I have been doing so far


Ensembles d'études connexes

Multiplication and division by 10, 100, 1000 - Set A

View Set

Article 225 Question Bank With Answers

View Set

Geometry Honors - PROOFS: Reflexive Property, Vertical Angles, Midpoint/Bisector/Medians, Angle Bisectors/CPCTC, Altitudes, Isosceles Triangles, Parallel Lines, Similarity

View Set

ITE 221 - PC Hardware & O/S Architecture - Exam 1

View Set

Test 2 - Bible: Personal Evangelism

View Set