Rails action to send binary data
I have one old application where the attachment is store as an id in the filesystem. The customer wants to have the attachment with the right name. So I have made this small action which accepts the id of the requested object and send the file data.
def send_bios
redirect_to :action => 'bios' if params[:id].blank?
@artist = Artist.find(params[:id]);
contents = File.open(@artist.bios_path,"rb") {|io| io.read}
send_data contents, :filename => "#{@artist.name}.pdf", :type => "application/pdf", :disposition => 'attachment'
end