How to Make Rails Endpoint Return Downloadable File
Suppose you want an endpoint that returns a file that the browser would open a “file download” dialogue. You can do so by setting the Content-Disposition.
Example:
class FooController < ApplicationController def show response.headers["Content-Disposition"] = "attachment; filename=foo.txt" render plain: "foobar" end end
In the example, when hitting the show path, most browsers will pop open a download box asking to save "foo.txt". Note, that you need to pass in "Content-Disposition" (string) and not :content_disposition (symbol).