Skip to content

Instantly share code, notes, and snippets.

@pzi
Created March 26, 2013 08:53
Show Gist options
  • Save pzi/5243994 to your computer and use it in GitHub Desktop.
Save pzi/5243994 to your computer and use it in GitHub Desktop.
Force download files in Ruby on Rails
= link_to 'Download 1', download_path(:symbol1_name)
= link_to 'Download 2', download_path(:symbol2_name)
class DownloadController < ApplicationController
RESOURCES = {
symbol1_name: 'filename1.ext',
symbol2_name: 'filename2.ext'
}
def index
# render view
end
def show
resource = RESOURCES[params[:id].to_sym]
if resource
send_file File.join('path', 'to', 'file', resource)
else
render nothing: true, status: '404'
end
end
end
Application.routes.draw do
resources :download, only: [:index, :show]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment