Latest / CyberCode Academy / Course 37 - Building Web Apps with Ruby On Rails | Episode 2: Navigating the Framework of Frameworks
Transcript
- 0:00Speaker 1: In the two hundred milliseconds or so it takes to
- 0:02Speaker 1: you to blink, a modern web application has already executed
- 0:06Speaker 1: millions of instructions millions.
- 0:08Speaker 2: It's really hard to wrap your head around it is.
- 0:10Speaker 1: I mean, you type a URL, hit enter, and before
- 0:13Speaker 1: your finger even lifts off the keyboard, global traffic has
- 0:16Speaker 1: been routed, a highly secure vault has been queried, complex
- 0:20Speaker 1: data translated, and a custom interface painted specifically for you.
- 0:24Speaker 2: And you just see the page load. Right. It feels
- 0:26Speaker 2: completely like magic.
- 0:28Speaker 1: It does, But today we are tearing down that illusion.
- 0:32Speaker 1: Welcome to our deep dive into the hidden assembly line
- 0:35Speaker 1: of a modern web request.
- 0:37Speaker 2: Yeah, because it happens so fast, we just take the
- 0:39Speaker 2: architecture completely for granted. But when you understand the sheer
- 0:42Speaker 2: volume of operations happening in that fraction of a second,
- 0:45Speaker 2: I mean, it completely changes how you view the web absolutely.
- 0:49Speaker 1: So to guide us today, we're going to mentally trace
- 0:51Speaker 1: this exact assembly line using the mechanics of Ruby on Rails. Now,
- 0:56Speaker 1: Rails is massive, It powers some of the biggest applications
- 0:59Speaker 1: out there. So what stood out to me right away
- 1:01Speaker 1: in the material is that Rails isn't actually just one
- 1:03Speaker 1: single tool.
- 1:04Speaker 2: Right, not at all. It's what we call a meta framework.
- 1:07Speaker 2: So it's a framework built out of several distinct specialized frameworks,
- 1:12Speaker 2: and all of those pieces are organized around this very
- 1:15Speaker 2: specific architectural pattern called MVC, which.
- 1:18Speaker 1: Stands for model view controller.
- 1:20Speaker 2: Exactly, model view controller.
- 1:22Speaker 1: Okay, let's unpack this MVC thing for a second, because
- 1:25Speaker 1: we hear that acronym thrown around constantly in software development.
- 1:29Speaker 1: Why is everything broken up into these three specific buckets.
- 1:33Speaker 2: Well, to understand why NBC is so crucial, you really
- 1:37Speaker 2: have to look at how web development used to be done.
- 1:39Speaker 2: In the early days, Developers would write files where the
- 1:42Speaker 2: logic that queried the database was tangled right up with
- 1:46Speaker 2: the HTML code that displayed the page.
- 1:49Speaker 1: Oh right, what people call spaghetti code.
- 1:51Speaker 2: Exactly spaghetti code. So if you wanted to just change
- 1:54Speaker 2: the color of a button, you accidentally ran the risk
- 1:55Speaker 2: of breaking the code that processed a user's credit card.
- 1:58Speaker 1: Oh wow, that sounds terrifying.
- 2:01Speaker 2: It was. It was completely unmintainable.
- 2:03Speaker 1: So NBC was the solution to untangle the spaghetti. You
- 2:06Speaker 1: isolate the data of the model, you isolate the user interface,
- 2:10Speaker 1: the view, and then you put a traffic cop right
- 2:13Speaker 1: in the middle to manage the communication between them. Yeah,
- 2:15Speaker 1: that's the.
- 2:16Speaker 2: Controller precisely, and in Rails that communication always starts at
- 2:20Speaker 2: the very front door of the application, the router, right.
- 2:23Speaker 1: I like to think of the router like the host
- 2:25Speaker 1: at a wildly busy restaurant. Like you walk in a
- 2:29Speaker 1: front door, that's your web request arriving at the server.
- 2:31Speaker 1: The host looks at you, figures out what you need,
- 2:34Speaker 1: and says, ah, you need a table for two. Let
- 2:36Speaker 1: me take you to this specific waiter who handles that section.
- 2:39Speaker 2: That's a great analogy. And in the Reils ecosystem, that
- 2:42Speaker 2: restaurant host is part of a core framework called action pack.
- 2:45Speaker 1: Action pack got it. Yeah.
- 2:46Speaker 2: Action pack is the ultimate traffic cop It handles the routing,
- 2:50Speaker 2: the controllers, and the views. When your request arrives, action
- 2:54Speaker 2: pack looks at a specific file where the developer has
- 2:57Speaker 2: basically mapped out all the acceptable paths.
- 3:00Speaker 1: Say, we want a really simple rule. If someone navigates
- 3:03Speaker 1: to our site and types in slash learn, we just
- 3:06Speaker 1: want to send them away to another website like some
- 3:08Speaker 1: external tutorial.
- 3:09Speaker 2: Sure, so in your routes file you define that using
- 3:12Speaker 2: a routing method like get. You conceptually tell the application. Hey,
- 3:16Speaker 2: when you receive a get request for the path, learn
- 3:20Speaker 2: redirect the user to this external URL.
- 3:24Speaker 1: And that's just a one liner.
- 3:25Speaker 2: Just a one liner. It's super clean.
- 3:26Speaker 1: But most of the time, I mean, we aren't kicking
- 3:29Speaker 1: people out of our app. We want to keep them inside.
- 3:31Speaker 1: So let's say we're building a system to manage a
- 3:34Speaker 1: list of people. A user goes to slash people. What happens?
- 3:38Speaker 2: Then in that case, instead of an external redirect, you
- 3:42Speaker 2: route that path to an internal controller. You point it
- 3:45Speaker 2: to your people controller and specifically to an action inside it,
- 3:48Speaker 2: like an index action that lists everyone out.
- 3:51Speaker 1: Ah. So the controller is the waiter in the restaurant
- 3:53Speaker 1: analogy exactly.
- 3:54Speaker 2: The router seats the desk, hands them off to the controller,
- 3:57Speaker 2: and now it's the controller's job to figure out what
- 3:59Speaker 2: the user at actually ordered.
- 4:00Speaker 1: Here's where I start thinking about scale, though. If I'm
- 4:03Speaker 1: building a whole application around people, I'm gonna need way
- 4:07Speaker 1: more than just a list page. I need a route
- 4:10Speaker 1: to show, a form to create a new person, a
- 4:13Speaker 1: route to view a specific profile, routes to update their info,
- 4:17Speaker 1: delete them, all of that.
- 4:18Speaker 2: Oh, for sure.
- 4:19Speaker 1: Before modern frameworks didn't developers have to manually write and
- 4:23Speaker 1: connect individual routing logic for every single one of those
- 4:26Speaker 1: actions they did.
- 4:27Speaker 2: Yeah, you would spend hours writing dozens of lines of
- 4:30Speaker 2: repetitive boilerplate code just to get the basic scaffolding of
- 4:34Speaker 2: a feature working. It was incredibly tedious.
- 4:37Speaker 1: And this is where Rails introduces what looks like absolute magic,
- 4:40Speaker 1: because instead of manually writing all those out you use
- 4:43Speaker 1: a single command called resources. You literally just tell the
- 4:46Speaker 1: routes that resources that people, Yeah.
- 4:48Speaker 2: And that one line instantly generates all the necessary routes.
- 4:52Speaker 2: It's a massive leap in developer productivity.
- 4:54Speaker 1: That's crazy just from that one.
- 4:56Speaker 2: Worse, it's that one declaration. Rails automatically assumes you want
- 4:59Speaker 2: a full suite of CRUD.
- 5:01Speaker 1: Routes CRU meaning create, read, update, and delete.
- 5:05Speaker 2: Right, it maps out the exact URLs and HTTP methods
- 5:09Speaker 2: needed to handle viewing a list, viewing a single person, creating, editing, destroying.
- 5:15Speaker 2: It wires all of those up to the corresponding actions
- 5:18Speaker 2: in your controller completely behind the scenes.
- 5:20Speaker 1: Okay, but let me push back a bit here. If
- 5:23Speaker 1: the framework is magically generating all these routes invisibly behind
- 5:26Speaker 1: a single line of code, doesn't become incredibly easy to
- 5:30Speaker 1: lose track of what actually exists. Like, you open your code,
- 5:33Speaker 1: you see resources dot people, But how do you actually
- 5:36Speaker 1: know what specific URLs the application is listening for.
- 5:40Speaker 2: That is a very common pitfall actually when you write
- 5:43Speaker 2: heavily on framework magic. But Rails provides a built in
- 5:46Speaker 2: truth teller.
- 5:47Speaker 1: Okay, what is it?
- 5:47Speaker 2: Through the command line interface, you can run a command
- 5:50Speaker 2: called Rails roads, and when you do that, the application
- 5:53Speaker 2: strips away all the abstraction and prints out a highly detailed,
- 5:56Speaker 2: formatted list of every single route that is currently active.
- 5:59Speaker 1: Oh that's anny.
- 6:00Speaker 2: Yeah, shows you the exact HTTP verb, the specific URL pattern,
- 6:03Speaker 2: and the exact controller action it triggers. It's basically how
- 6:06Speaker 2: you verify what the traffic cop actually knows.
- 6:09Speaker 1: Okay, so our web request has successfully talked to the
- 6:12Speaker 1: traffic cop and it's been handed off to the controller.
- 6:15Speaker 1: The user asked for a list of people, but the
- 6:18Speaker 1: controller is currently empty handed. It needs to actually fetch
- 6:21Speaker 1: that list, which means it has to talk to the database.
- 6:24Speaker 2: Yes, the database is the vault where the actual information
- 6:27Speaker 2: is locked away. But there is a massive translation problem
- 6:31Speaker 2: here that we have to deal with.
- 6:32Speaker 1: Right, because your application logic is written in the fluid,
- 6:35Speaker 1: elegant language of Ruby, but your database vault only understands
- 6:40Speaker 1: the rigid, highly structured language of SQL.
- 6:43Speaker 2: Exactly, they don't speak the same language.
- 6:45Speaker 1: So we need a translator. And in RAILS, that translator
- 6:49Speaker 1: is a framework called active record. It performs what's known
- 6:52Speaker 1: as object relational mapping or an ARM right.
- 6:55Speaker 2: Active record acts as a diplomatic translator between the two
- 6:58Speaker 2: systems because writing raw queries by hand is notoriously messy
- 7:02Speaker 2: and it's really prone to errors, especially when the queries
- 7:05Speaker 2: get complex.
- 7:06Speaker 1: So what does the ORM actually do?
- 7:07Speaker 2: What an ORM does is seamlessly map the data stored
- 7:11Speaker 2: in a database road directly into a Ruby object in
- 7:14Speaker 2: the applications memory.
- 7:15Speaker 1: Oh so instead of writing a clunky sequel string that
- 7:18Speaker 1: says like update people set email equals new where aid
- 7:22Speaker 1: equals five. Yeah, you just talk to the Ruby object.
- 7:25Speaker 1: You tell your person object to update it's email. Yep.
- 7:27Speaker 2: An active record intercepts that simple Ruby command, instantly translates
- 7:31Speaker 2: it into the proper SEQL syntax, executes it against the database,
- 7:35Speaker 2: and reports back. It completely insulates the developer from the
- 7:39Speaker 2: database mechanics.
- 7:40Speaker 1: That's brilliant.
- 7:41Speaker 2: It is an active record actually manages this relationship across
- 7:44Speaker 2: four main roles migrations, validations, storage and retrieval, and callbacks.
- 7:49Speaker 1: Let's talk about migrations because they are fascinating. They are
- 7:52Speaker 1: essentially how we build the blueprints for the vault itself.
- 7:56Speaker 1: If we want to create a new database table to
- 7:58Speaker 1: hold our people, we don't go into the database software
- 8:01Speaker 1: and manually add columns do it.
- 8:03Speaker 2: No, you generate a migration file. You use the command
- 8:06Speaker 2: line and you tell Rails you want to create a
- 8:08Speaker 2: people table. Rails generates a blueprint file containing a block
- 8:12Speaker 2: of Ruby code, and inside that code you define your columns. Conceptually,
- 8:16Speaker 2: you say, add a column named email and make it
- 8:19Speaker 2: a string, or add a column for their age and
- 8:22Speaker 2: make it an integer.
- 8:23Speaker 1: And then you just run a command to migrate the database,
- 8:26Speaker 1: and active record translates your blueprint into the SQL commands
- 8:29Speaker 1: needed to physically build the table exactly.
- 8:32Speaker 2: And the safety net here is great. If you realize
- 8:35Speaker 2: you messed up, like maybe you forgot to add a
- 8:37Speaker 2: column for a phone number, you can just roll back
- 8:39Speaker 2: the migration. Active record perfectly reverses the operation and gives
- 8:43Speaker 2: you a clean slate.
- 8:44Speaker 1: Oh that's incredibly forgiving, And you'll notice something interesting when
- 8:47Speaker 1: you look at the final table, active record automatically adds
- 8:51Speaker 1: a column that you never explicitly asked for, an ID column.
- 8:55Speaker 2: Right. It automatically gives every single record a unique primary key.
- 9:00Speaker 2: This is that famous Rails philosophy of convention over configuration.
- 9:04Speaker 1: Because it assumes you need a primary key to track data, right,
- 9:07Speaker 1: because ninety nine percent of the time you do. It
- 9:09Speaker 1: saves you from configuring.
- 9:10Speaker 2: The obvious Exactly. Now, once the vault is built, you
- 9:14Speaker 2: have to protect what goes into it. If a user
- 9:16Speaker 2: is filling out a form, you don't want them saving
- 9:19Speaker 2: a profile without a name or an email address, right,
- 9:22Speaker 2: Which brings us to the second role of active record validations.
- 9:26Speaker 2: In your Ruby code, you just set a simple rule
- 9:29Speaker 2: validate that the name is Presence.
- 9:31Speaker 1: Wait, let me stop you there. Why are we doing
- 9:33Speaker 1: that in the Ruby application layer. Doesn't the database already
- 9:36Speaker 1: have its own strict rules, Like you can configure a
- 9:40Speaker 1: database column to absolutely reject any blank entries. It feels
- 9:44Speaker 1: like we are duplicating work by writing it in the
- 9:46Speaker 1: application code too.
- 9:48Speaker 2: You definitely can, and for ultimate security you often should
- 9:51Speaker 2: have constraints at the database level. But think about the
- 9:54Speaker 2: user experience. If you rely purely on the database to
- 9:57Speaker 2: catch the error. The database violent only rejects the bad
- 10:01Speaker 2: data and throws a massive, highly technical error back at
- 10:04Speaker 2: the application. By a time it bubbles up, the user
- 10:06Speaker 2: experience is ruined.
- 10:08Speaker 1: Ah. So by putting the validation in the active record
- 10:11Speaker 1: translator layer, we intercept the bad data before it ever
- 10:14Speaker 1: touches the vault doors exactly.
- 10:17Speaker 2: Active record stops the save flags the missing name and
- 10:20Speaker 2: provides a clean, readable error message. We can instantly pass
- 10:24Speaker 2: that back to the screen. Hey, you forgot to enter
- 10:26Speaker 2: your name. It provides immediate, graceful feedback.
- 10:30Speaker 1: That makes perfect sense. And once the data is fully
- 10:32Speaker 1: vetted and successfully saved, active record has one more trick,
- 10:36Speaker 1: right callbacks.
- 10:37Speaker 2: Yeah. Callbacks are great. They allow you to hook additional
- 10:40Speaker 2: automated tasks to the life cycle of the data.
- 10:43Speaker 1: Like telling the system the absolute instant a new person
- 10:47Speaker 1: is successfully shaved into the vault for the very first time,
- 10:50Speaker 1: triggers separate process to send them a welcome email.
- 10:52Speaker 2: Right, It's all about seamlessly orchestrating the side effects of
- 10:56Speaker 2: changing data.
- 10:57Speaker 1: Okay, so let's recap where we are the controller app
- 11:00Speaker 1: active record for the list of people. Active record translated
- 11:03Speaker 1: the request fetch the data from the vault and handed
- 11:05Speaker 1: it back. But if you hand a user raw database code,
- 11:09Speaker 1: they're going to immediately close the browser tap.
- 11:11Speaker 2: Yeah, nobody wants to see that.
- 11:12Speaker 1: So how does Rails actually translate that raw data into
- 11:15Speaker 1: the beautiful visual interface.
- 11:17Speaker 2: We expect to paint the screen? Action Pack uses another
- 11:21Speaker 2: subframework called action View. But first there's a handoff that
- 11:24Speaker 2: has to happen. The controller has the data, but the
- 11:27Speaker 2: view is a completely separate.
- 11:29Speaker 1: File, and Rails bridges this gap almost invisibly using instance variables.
- 11:35Speaker 1: In the controller, you assign your data to a specifically
- 11:37Speaker 1: marked variable, and Rails performs a bit of magic. It
- 11:40Speaker 1: takes that variable and perfectly mirrors it over to the
- 11:43Speaker 1: view's context.
- 11:44Speaker 2: Yeah, so the view instantly has access to the exact
- 11:47Speaker 2: same data the controller just fetched, and it does this
- 11:50Speaker 2: with security in mind too. How So, Well, when data
- 11:53Speaker 2: flows the other way, say the user is submitting a
- 11:55Speaker 2: form back to the controller, Rails uses a strict vetting system.
- 12:00Speaker 2: Iplicitly requires that the incoming data contains the exact expected
- 12:03Speaker 2: parameters like a name and an email, and it aggressively
- 12:06Speaker 2: filters out any unexpected or potentially malicious data fields before
- 12:10Speaker 2: it ever processes them.
- 12:11Speaker 1: So, assuming we securely pass our list of people to
- 12:14Speaker 1: the view, we need to build the HTML and action
- 12:17Speaker 1: view handles this using a syntax called ERB or embedded
- 12:21Speaker 1: rub right.
- 12:22Speaker 2: ERB is brilliant because it allows you to mix standard
- 12:24Speaker 2: static HTML tags with dynamic Ruby logic in the viewfile.
- 12:29Speaker 2: Rails uses a clever tagging system essentially drop little markers
- 12:33Speaker 2: into your HTML to tell the server what to do.
- 12:35Speaker 1: So instead of writing out twenty individual list items in
- 12:38Speaker 1: HTML for twenty people, you use an ERB marker to
- 12:42Speaker 1: create a loop. Some of these markers basically tell Rails, hey,
- 12:46Speaker 1: run this code silently behind the scenes. That's your loop logic, yep.
- 12:50Speaker 2: But other markers, the ones with an equal sign, act
- 12:52Speaker 2: like a paintbrush. They tell Rails take the result of
- 12:55Speaker 2: this variable and literally painted into the HTML document right
- 12:59Speaker 2: here exactly.
- 13:00Speaker 1: You use that paint marker inside your loop to dynamically
- 13:03Speaker 1: insert the person's name for every single record in the database.
- 13:07Speaker 1: When the server is done processing, all those little Ruby
- 13:10Speaker 1: markers vanish and the browser receives a perfectly standard HTML document.
- 13:14Speaker 2: Right But you know, a web page is more than
- 13:16Speaker 2: just a list. It has a navigation bar, a footter metadata.
- 13:19Speaker 2: You don't want to rewrite all of that structural HTML
- 13:21Speaker 2: on every single page of your application.
- 13:23Speaker 1: Oh, absolutely not, which is why action view structurally divides
- 13:28Speaker 1: the interface into layouts and partials.
- 13:30Speaker 2: Yeah, let's explain that. A partial is just a reusable,
- 13:33Speaker 2: bite sized chunk of interface, like a specific user profile
- 13:37Speaker 2: card that you might want to display on the homepage,
- 13:39Speaker 2: the search page, and the user directory. You write it
- 13:42Speaker 2: once and inject it anywhere.
- 13:44Speaker 1: Here's where it gets really interesting, though. The layout the
- 13:47Speaker 1: big picture. I think of the layout like a heavy
- 13:50Speaker 1: wooden picture frame. It contains all the standard structure of
- 13:54Speaker 1: your site that never changes your top navigation, your foot
- 13:56Speaker 1: or your structural containers. And right in the middle of
- 13:59Speaker 1: that layout fileile is a single command, the yield command.
- 14:03Speaker 2: Oh I love that analogy.
- 14:04Speaker 1: Right, that yield command is where the specific view template
- 14:08Speaker 1: your request it gets injected. The framework swaps out the painting,
- 14:11Speaker 1: but it keeps the exact same picture frame.
- 14:13Speaker 2: And that picture frame. The layout also includes vital security
- 14:17Speaker 2: infrastructure by default. For example, it automatically injects hidden tokens
- 14:21Speaker 2: to protect against cross site request forgery or CSRF.
- 14:24Speaker 1: Let's explain CSRF quickly because the mechanism is really clever.
- 14:28Speaker 1: A malicious site might try to trick your browser into
- 14:31Speaker 1: submitting a form on your application's behalf like tricking you
- 14:35Speaker 1: into deleting your account while you're browsing a different tab.
- 14:38Speaker 1: How does RAILS stop that?
- 14:40Speaker 2: It stops it by hiding a unique, secret, cryptographically secure
- 14:44Speaker 2: token inside every single legitimate form rendered by action View.
- 14:48Speaker 2: Oh okay, so when a form is submitted back to
- 14:51Speaker 2: the server, Rails checks for that specific token. Because a
- 14:54Speaker 2: malicious third party site cannot read that hidden token from
- 14:57Speaker 2: your page, their forged request will be missing it, and
- 15:00Speaker 2: Wales will instantly reject the submission.
- 15:03Speaker 1: So it builds the walls of the fortress automatically. It
- 15:05Speaker 1: also sets up strict content security policies to guard against
- 15:08Speaker 1: unauthorized JavaScript executing. But speaking of the browser, we have
- 15:12Speaker 1: to talk about styling. Action View ties into the asset
- 15:15Speaker 1: pipeline to manage all your CSS files.
- 15:17Speaker 2: Yes, instead of making the browser download twenty different CSS
- 15:21Speaker 2: style sheets, the asset pipeline compiles all of your styling
- 15:24Speaker 2: into one single Mini five file.
- 15:27Speaker 1: And there is an incredibly smart caching trick here. Browsers
- 15:31Speaker 1: are aggressively lazy they want to save CSS files locally
- 15:34Speaker 1: so they don't have to download them over and over.
- 15:37Speaker 1: But if you update your app and change your background
- 15:39Speaker 1: to crimson red, how do you force every user's browser
- 15:42Speaker 1: to realize there's a new file.
- 15:44Speaker 2: Rail solves this by adding a unique cryptographic fingerprint to
- 15:47Speaker 2: the end of the compiled CSS file name. It's a
- 15:50Speaker 2: long string of characters based entirely on the contents of
- 15:53Speaker 2: the file.
- 15:54Speaker 1: So the absolute second you change that background color to
- 15:56Speaker 1: crimson red, the file content's change, which means the finger
- 16:00Speaker 1: it changes exactly.
- 16:01Speaker 2: The browser sees a completely new file name, realizes its
- 16:05Speaker 2: cased version is obsolete, and instantly downloads the fresh styling.
- 16:09Speaker 1: So we've queried the database, translated the data, embedded it
- 16:12Speaker 1: into htmail, and styled it with a beautifully cash CSS
- 16:15Speaker 1: file we sent it to the user. But if we're
- 16:17Speaker 1: building a modern web application, users don't just want fast
- 16:21Speaker 1: loading times. They want the app to feel instantaneous, like
- 16:24Speaker 1: a native piece of software on their phone, and to.
- 16:26Speaker 2: Achieve that instantaneous feel in the browser, we have to
- 16:30Speaker 2: leverage JavaScript. Rails natively supports modern JavaScript bundling through Webpack,
- 16:35Speaker 2: allowing you to seamlessly integrate massive complex third party JavaScript
- 16:40Speaker 2: libraries that.
- 16:41Speaker 1: Also use it something called unobtrusive JavaScript right to handle lightweight,
- 16:46Speaker 1: invisible interactions.
- 16:47Speaker 2: Yeah, things like automatically popping up a confirmation dialogue when
- 16:51Speaker 2: you click delete, or temporarily disabling a submit button so
- 16:55Speaker 2: a user doesn't accidentally double click and charge their credit
- 16:58Speaker 2: card twice.
- 16:58Speaker 1: But the real star the show here. The mechanism that
- 17:01Speaker 1: gives rails apps that lightning fast single page application feel
- 17:06Speaker 1: without actually being one is turble links. The first time
- 17:09Speaker 1: you realize what turblelnks is doing, it feels like an
- 17:11Speaker 1: absolute magic trick.
- 17:13Speaker 2: It really does. To understand the magic, think about how
- 17:16Speaker 2: standard navigation works. When you click a link to go
- 17:18Speaker 2: to a new page, your browser completely throws away the
- 17:21Speaker 2: current page. The screen flash is white, The browser downloads
- 17:24Speaker 2: the new HTML, reparses all the CSS, reevaluates all the JavaScript,
- 17:29Speaker 2: and redraws the entire windle from absolute scratch.
- 17:32Speaker 1: It's a hard refresh, and it's computationally expensive, very expensive.
- 17:36Speaker 1: But with turble links enabled, it fundamentally hijacks that process.
- 17:40Speaker 1: It intercepts your click. Instead of letting the browser do
- 17:43Speaker 1: a hard refresh, turble links quietly fires off an invisible
- 17:47Speaker 1: background request to the server to fetch the new page.
- 17:50Speaker 2: Right, and when that new HTML arrives, turblns inspects it.
- 17:54Speaker 2: It completely ignores the header section of the new document
- 17:57Speaker 2: because it knows the CSS and JavaScript already loaded and active. Right,
- 18:02Speaker 2: It grabs only the body of the new page, and
- 18:04Speaker 2: it seamlessly swaps it out with the current body on
- 18:06Speaker 2: your screen. Finally, it silently updates the URL in your
- 18:10Speaker 2: address bar, so your browser history stays perfectly intact.
- 18:14Speaker 1: The perceived loading speed is astonishing because the browser never
- 18:17Speaker 1: flashes white, never stops to recompile style sheets. It just
- 18:21Speaker 1: instantly swaps the painting inside the frame. Wait a minute, though,
- 18:24Speaker 1: so what does this all mean for our code? If
- 18:27Speaker 1: the browser never does a true hard refresh, doesn't that
- 18:32Speaker 1: fundamentally break custom JavaScript? Usually developers write scripts that listen
- 18:37Speaker 1: for the window loaded event before they run. If turbolinks
- 18:40Speaker 1: is just sneaking in and swapping the body, those standard
- 18:43Speaker 1: load events are never going to fire.
- 18:45Speaker 2: You nailed it. That is the ultimate pitfall for developers
- 18:49Speaker 2: new to this ecosystem. If you write standard JavaScript that
- 18:52Speaker 2: waits for the window to load, it will run perfectly
- 18:54Speaker 2: the very first time you visit the site. But the
- 18:57Speaker 2: second you click a link and turble Links takes over,
- 18:59Speaker 2: your customer scripts will go completely dead.
- 19:01Speaker 1: Because the window never technically reloaded. So how do we
- 19:04Speaker 1: fix it?
- 19:04Speaker 2: Well, the framework provides a custom event listener to solve
- 19:07Speaker 2: this exact problem. Instead of telling your JavaScript to wait
- 19:10Speaker 2: for the standard browserload event, you wrap your code in
- 19:13Speaker 2: a special handler that listens for a turbolinks dot load event.
- 19:16Speaker 1: See.
- 19:17Speaker 2: Because the framework intentionally fires that specific event every single
- 19:21Speaker 2: time it swaps out the body, your code runs flawlessly
- 19:24Speaker 2: and reliably on every single navigation, regardless of whether it
- 19:28Speaker 2: was a hard refresh or turbolink swap.
- 19:30Speaker 1: It's an incredibly elegant system once you understand how the
- 19:33Speaker 1: pieces connect. So as we wrap up this deep dive,
- 19:36Speaker 1: I have a mental exercise for you, the listener, Yeah,
- 19:38Speaker 1: to really reinforce what we've unpacked today. I want you
- 19:41Speaker 1: to try this the next time you are using your
- 19:44Speaker 1: favorite web application. Maybe you click a button to update
- 19:47Speaker 1: your profile settings or save a new post. I want
- 19:50Speaker 1: you to visually trace the hidden assembly line.
- 19:53Speaker 2: That's a great exercise.
- 19:54Speaker 1: Imagine action pack at busy restaurant host intercepting your request
- 19:58Speaker 1: and directing Trafficmagine active record the diplomatic translator, validating your inputs,
- 20:03Speaker 1: catching errors and seamlessly translating your data into the vault.
- 20:07Speaker 2: And then actionview right, imagine.
- 20:09Speaker 1: Action view taking that raw data, embedding it into an
- 20:13Speaker 1: HTML template via ERB markers, wrapping it in your secure
- 20:17Speaker 1: layout and styling it. And finally, imagine turbolinks catching that
- 20:20Speaker 1: response and instantaneously swapping out the HTML on your screen
- 20:24Speaker 1: so fast you didn't even notice a transition.
- 20:27Speaker 2: It really is a masterclass in architectural orchestration. The request
- 20:30Speaker 2: flows from the controller to the model, to the view
- 20:32Speaker 2: and back to the browser in fractions of a second.
- 20:35Speaker 2: MVC in perfect motion.
- 20:37Speaker 1: It is. But I want to leave you with one
- 20:39Speaker 1: final provocative thought to ponder. We've seen today how metaframeworks
- 20:43Speaker 1: provide immense, almost staggering power. They hide incredibly complex operations
- 20:49Speaker 1: like writing raw sequal schemas, or handling browser caching, or
- 20:52Speaker 1: manually intercepting page loads behind simple magic one liners.
- 20:56Speaker 2: Which brings up the eternal tension in software development. Does
- 20:59Speaker 2: relying on this framework magic make us better and more
- 21:02Speaker 2: capable builders because it allows us to move incredibly fast
- 21:05Speaker 2: and focus purely on the user experience, or does it
- 21:09Speaker 2: risk making us overly dependent on abstracted tools we don't
- 21:12Speaker 2: fully understand underneath, because inevitably, when the magic breaks, someone
- 21:16Speaker 2: has to know how to fix the underlying machinery.
- 21:19Speaker 1: A great question for any self taugh learner or a
- 21:21Speaker 1: professional to chew on. Next time you hit and earn
- 21:23Speaker 1: a URL, remember it's not magic. It's just an incredibly fast,
- 21:27Speaker 1: beautifully designed assembly line.