Reputation: 163
I recently made a website for someone in which they want to have a free download of a card game that they made in the form of a .zip file.
I have the download working but was wondering if there is a "correct" place to put public downloadable files according to Rails. When I put the .zip file in my public folder and change my download_cards method to reflect the changes I get an "unable to locate file" error message.
This is my setup for the download:
Controller:
class DownloadsController < ApplicationController
def download_cards
send_file "#{Rails.root}/app/assets/downloads/cardgame.zip", type: "application/zip", x_sendfile: true
end
end
Routes:
Rails.application.routes.draw do
root 'static_pages#home'
get 'download_cards', to: "downloads#download_cards"
end
View:
<%= link_to "download the cardz", download_cardz_path %>
Upvotes: 2
Views: 1965
Reputation: 1124
You could place the file directly in the public/
folder and then use
<%= link_to "Download Card Game", "/cardgame.zip" %>
This will create a the following link
<a href="/cardgame.zip">Download Card Game</a>
When Rails receives GET /cardgame.zip
it will check the public/
directory for a matching file before looking for a route that matches. No need for a controller at all to serve static files.
Upvotes: 7