In a Ruby on Rails app I was working on, I wanted to include a link that would download a user’s data (bank statements in this case) as JSON.
Originally I had the following ERB:
| |
Clicking this link would show JSON in the browser, but it wouldn’t download a JSON file, which is what I wanted.
The solution was to add a download attribute to the ERB snippet:
| |
Full Setup
For additional context, here is all the code required to get this to work.
First I created this method in statements_controller.rb:
| |
Then I added a new route to routes.rb:
| |
And then I added the link_to helper in the view:
| |
The link_to ERB helper is creating this HTML:
| |
The download attribute can be customized in ERB. For example, I can name the file based on the user’s name like so:
| |
And it will generate the following HTML:
| |
This link will trigger a download for nelson-statements.json in my case.
Ultimately this was a case of understanding the download HTML attribute for <a> tags and how to use it in Rails.
Further Reading and References
Note that there are other ways of handling this, like using the send_data method in a controller. But that’s beyond the scope of this post. Read more about it here:
https://api.rubyonrails.org/v7.0.1/classes/ActionController/DataStreaming.html#method-i-send_data
References: