How to display files uploaded using carrierwave, fog and local storage with rails application? -


i using carrierwave fog create simple image uploading process on server. goal have images stored in server in folder :

/opt/myapp/uploads/ 

i have configured carrierwave , fog parameters , upload working :

carrierwave.configure |config|    config.fog_credentials = {     :provider               => 'local',      :local_root             => '/opt/myapp/'    }   config.fog_public     = false                  config.fog_directory  = 'uploads/'     config.storage = :fog   config.asset_host = proc |file|     '/opt/myapp/uploads/'   end end 

when upload image can see stored in corresponding folder. how can display them in web pages ? generated url is

http://localhost:3000/opt/myapp/uploads/<path-to-my-image>.png 

so application tries images opt/ folder in rails application directory, how tell retrieve them server filesystem instead ?

ok,that easy :

first add route corresponding urls :

match '/opt/myapp/uploads/:file_name' => 'files#serve' 

the create filescontroller serve method :

class filescontroller < applicationcontroller    def serve     before_filter :authenticate_user! #used devise protect access images     path = "/opt/myapp/uploads/#{params[:file_name]}.png"      send_file( path,       :disposition => 'inline',       :type => 'image/png',       :x_sendfile => true )   end end 

then needed add line in development.rb , production.rb configuration files :

config.action_dispatch.x_sendfile_header = "x-accel-redirect" #to use thin , nginx 

Comments