Learn to render a static file as a response to a request. Static files can be HTML files, but most often they are media files such as PDFs, images, or video.
- [Instructor] In this movie, we're going to learn how to render static files using Sinatra. We've already talked about how you can match routes in Sinatra. So you have a matching route and it does a block of code. What we haven't talked about is that before Sinatra goes and looks at those routes, it actually does something else. It searches inside the public directory. So if we have a public directory, it looks in there to find whatever files are there and see if one of those files matches the URL. If not, then it goes and it checks the routes to see if we have a matching route.
We could handle it ourselves inside one of those matching routes. We could have a code block and ask that code block to return a static file. If we wanted to do it this way and handle it ourselves, then we would just need to write some additional Ruby code to do that. Ruby gives us a method called File Read, which will open up a file, read its contents into memory, and then close the file. At that point it's a string that we can work with. We can assign it to variables and things like that. In this first example, I'm just simply reading the file into a string and then returning it back to the user automatically.
In the second example I'm doing the same thing but I wanted to show you that most of the time, you're going to want to be able to build up the file path in order to locate the file. File.join is the best way for you to do that. It puts together those strings in a very safe way inside Ruby so we have a file path, and then we can just pass that to File Read. Now File Read is simply pure Ruby. There's also a helper method in Sinatra called send_file. It does the exact same thing, except that it does one additional thing as well, which is that it correctly sets the content type of the response.
This matters if you're sending back an asset to the user, like an image or a PDF or a movie file, or something like that. With the File Read technique, the response is going to be a string. And so it's going to automatically have the content type of HTML. That's what being sent back to the user is some HTML. But if we're sending back a PDF, we may not want the content type to be HTML. We may want it to be the content type that matches for PDFs. Well, send_file will figure that out for us and send back the appropriate content type.
We also still have to specify the full file path here. In the second example, I'm showing you that you can simply concatenate those strings together and get the same result. File.join is the more technically correct way to do it in Ruby, but most of the time, just cancatenating the string works as well. In order to illustrate how this works with static files, let's start with something that could very well be a real world example. Let's imagine that we have an original basic HTML website that's already built, and now we want to port that into Sinatra so we can start to make dynamic pages out of it.
If you remember, we only have this default route set right now. Without doing any additional configuration, let's start up our Sinatra server. Ruby explore_california/sinatra.rb. And then once it's listening let's go over here into Firefox. Now if I hit return here, you'll see that I get Explore California. That's still my root route. But now, notice if I do forward slash index.html and I hit return. Now look at that. I get the new website and I can click around, I can see all the different pages.
Now you'll remember that that's not going to work for us because we also need to tell if it's inside that public directory. Let's do filepath=File.join. Let's join together settings.public_folder and then comma, and then index.html. That will join it together for us and then file path just has to be read here. That's going to read it in as a string and then return it back to the user. Save it, let's stop Sinatra and restart it.
And once it restarts let's come back over and let's reload our page. The root route. And you'll see that we now get back that index.html page. We handled it explicitly in our code. Just so we can see how it works, let's try the same thing. I'm going to take file.read and just comment it out, and instead let's do send_file with filepath. The file path is the same. We're just going to call send_file instead. Let's stop terminal.
And lets restart Sinatra. We'll come back over and go into Firefox and let's reload our page one more time and see that it still works there. Again, if we're working with HTML, it doesn't matter which one of those we use. File read and send_file do the exact same thing. If we were sending back some different kind of content besides HTML, then it would make a difference. Then we would want to use send_file for sure.
- Installing Sinatra
- Launching the server
- Defining routes
- Using route patterns and parameters
- Redirecting requests
- Rendering dynamic templates
- Using partial templates and layouts
- Defining custom helpers