Hi, i have made a file uploading function on my application, the thing
is
i want to allow my users to download the file (can be pdf, doc, img,
etc)
so in my file upload controller:
def download
@photo = Photo.find(params[:id])
send_file("#{RAILS_ROOT}/public/"[email protected],
:filename => @photo.filename,
:type => @photo.content_type,
:disposition => ‘attachment’)
end
this works perfectly fine, BUT only if the file actually exists in
…/public/…
i noticed that when i try to upload a file using the app,
it doesn’t automatically store it into the public folder.
if i manually copy paste the file into the public folder, the download
works fine.
this is my controller, (notice that the new & create methods doesn’t say
anything about storing the uploaded files into the public folder)
class PhotoAdminController < ApplicationController
def index
list
render :action => ‘list’
end
GETs should be safe (see
URIs, Addressability, and the use of HTTP GET and POST)
verify :method => :post, :only => [ :destroy, :create, :update ],
:redirect_to => { :action => :list }
def list
@photo_pages, @photos = paginate :photos, :per_page => 10
end
def show
@photo = Photo.find(params[:id])
end
def new
@photo = Photo.new
end
def create
@photo = Photo.new(params[:photo])
if @photo.save
flash[:notice] = ‘Photo was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end
def edit
@photo = Photo.find(params[:id])
end
def update
@photo = Photo.find(params[:id])
if @photo.update_attributes(params[:photo])
flash[:notice] = ‘Photo was successfully updated.’
redirect_to :action => ‘show’, :id => @photo
else
render :action => ‘edit’
end
end
def destroy
Photo.find(params[:id]).destroy
redirect_to :action => ‘list’
end
def download
@photo = Photo.find(params[:id])
send_file("#{RAILS_ROOT}/public/images/"[email protected],
:filename => @photo.filename,
:type => @photo.content_type,
:disposition => ‘attachment’)
end
end
can someone teach me how to code my app to store the uploaded file into
the public folder?
thank you very much in advance!