if I load the file in my app on Heroku, everything works, but when I tried again to reload the application, it gives me the following error:
2013-01-25T08:48:31+00:00 app[web.1]: app/controllers/main_controller.rb:20:in `index'
2013-01-25T08:48:31+00:00 app[web.1]:
2013-01-25T08:48:31+00:00 app[web.1]:
2013-01-25T08:48:31+00:00 app[web.1]: Errno::ENOENT (No such file or directory - /app/config/cases/casesID6.yml):
2013-01-25T08:48:31+00:00 app[web.1]: app/controllers/main_controller.rb:20:in `read'
LOCALLY IT WORK FINE !
main controller:
# importo yaml di configurazione
require 'yaml'
if Survey.exists?(1)
#idOfSurvey = Survey.find {|s| s['active_state'] == true}['id']
nameOfSurvey = "casesID"+String(#idOfSurvey)+".yml"
#survey = YAML::load(ERB.new(IO.read(File.join(Rails.root, 'config/cases', nameOfSurvey))).result)
else
render :action => 'noYaml' and return
end
upload controller :
#inserisco il nuovo questionario
survey = Survey.new
if Survey.exists?(1)
n = String(Integer(Survey.maximum("Id"))+1)
survey.name = "casesID#{n}"
else
survey.name = "casesID1"
end
File.open(Rails.root.join('config/cases', survey.name+".yml"), 'wb+') do |file|
file.write(uploaded_io.read)
end
survey.save()
I believe that my file uploaded to Heroku, may be cleared by the platform for issues of memory, for example, or because it saves them as temporary, is this possible? solutions!
I repeat, everything works fine locally :(
Whilst you can write to the Heroku filesystem once you scale, restart or push a new version of your code then the file will be gone.
You need to use a persistant file store such as Amazon S3, Rackspace or such like - read more at https://devcenter.heroku.com/articles/dynos#ephemeral-filesystem and use a gem like CarrierWave or Paperclip which make connecting to S3 super simple.
Related
I have code to write a file to save a PDF and then upload it to s3. What I have works perfectly in dev. but not in production.
def upload_to_s3(pdf)
save_path = "#{Rails.root}/tmp/pdfs/invoice_#{#invoice.id}.pdf"
f = File.new(save_path, 'w:ASCII-8BIT')
f.write(pdf)
uploader = InvoiceUploader.new
File.open(save_path) { |file| uploader.store!(file) }
#invoice.update(pdf: uploader.url)
File.delete(save_path)
uploader.url
end
Stack Trace:
Rendered invoices/pdf.html.erb (6.5ms)
Completed 500 Internal Server Error in 1420ms (ActiveRecord: 34.6ms)
Errno::ENOENT (No such file or directory # rb_sysopen - /home/deploy/reputation/releases/20180401031049/tmp/pdfs/invoice_2.pdf):
app/controllers/api/v1/invoices_controller.rb:140:in `initialize'
[5e443877-e7fe-4848-847b-f5f6159e7db9]
app/controllers/api/v1/invoices_controller.rb:140:in `new'
[5e443877-e7fe-4848-847b-f5f6159e7db9]
app/controllers/api/v1/invoices_controller.rb:140:in `upload_to_s3'
[5e443877-e7fe-4848-847b-f5f6159e7db9]
app/controllers/api/v1/invoices_controller.rb:65:in `pdf'
The error message clearly states:
Errno::ENOENT (No such file or directory # rb_sysopen - /home/deploy/reputation/releases/20180401031049/tmp/pdfs/invoice_2.pdf): ...
Create the directory /home/deploy/reputation/releases/20180401031049/tmp/pdfs/ in production upfront. Since it’s dynamic (dependent on the release datetime,) it’s better to create it in your ruby code:
save_dir = "#{Rails.root}/tmp/pdfs/"
Dir.mkdir(save_dir)
save_path = ...
I'm using carrierwave to upload images from my rails app, and later pass that image to resque for resizing them in the background. The image gets uploaded properly. The problem when resque tries to resize it, mini_magick says that "No such file or directory"
This is my ImageController code which handles the upload
#create image and embed into story
def create
img_attr = params[:image]
img_attr[:media] = params[:image][:media].first if params[:image][:media].class == Array
image = Image.new img_attr
#story.images << image
if #story.save
Resque.enqueue(ImageQueue,image.id)
respond_to do |format|
format.json {
render :json => [image.to_jq_upload].to_json
}
end
else
render :json => [{:error => 'custom_failure'}], :status => 304
end
end
And this is my Resque code
class ImageQueue
#queue = :image_queue
def self.perform(image_id)
image = Image.find image_id
image.recreate_delayed_versions!
image.save
end
end
And the upload path is set here
def store_dir
"uploads/stories/#{model.viewable_id}/res"
end
This is the error stack that I get
No such file or directory - /uploads/stories/533d5b8756617390c0070000/res/636a8fe128.jpg
/Users/skmvasu/.rvm/gems/ruby-2.0.0-p451/gems/mini_magick-3.7.0/lib/mini_magick/image.rb:110:in `initialize'
/Users/skmvasu/.rvm/gems/ruby-2.0.0-p451/gems/mini_magick-3.7.0/lib/mini_magick/image.rb:110:in `open'
/Users/skmvasu/.rvm/gems/ruby-2.0.0-p451/gems/mini_magick-3.7.0/lib/mini_magick/image.rb:110:in `open'
/Users/skmvasu/repo/mangoweb/app/uploaders/image_uploader.rb:97:in `original_dimensions'
/Users/skmvasu/.rvm/gems/ruby-2.0.0-p451/gems/carrierwave-0.10.0/lib/carrierwave/uploader/processing.rb:84:in `block in process!'
/Users/skmvasu/.rvm/gems/ruby-2.0.0-p451/gems/carrierwave-0.10.0/lib/carrierwave/uploader/processing.rb:76:in `each'
/Users/skmvasu/.rvm/gems/ruby-2.0.0-p451/gems/carrierwave-0.10.0/lib/carrierwave/uploader/processing.rb:76:in `process!'
The weird thing is the same code works in my Linux box, and my production box which also runs Linux, but not on my new Mac. Is it a problem with ImageMagick? I'm installing it through Homebrew. I even tried uninstalling it and reinstalling it with source, but that didn't work either.
I'm not sure what I'm doing wrong here? Please help me solve this issue.
As per the error,
No such file or directory - /uploads/stories/533d5b8756617390c0070000/res/636a8fe128.jpg
Either 636a8fe128.jpg is not there at the given path i.e., /uploads/stories/533d5b8756617390c0070000/res or
/uploads/stories/533d5b8756617390c0070000/res one or all of the directories in this path are not there. I would recommend go to terminal and navigate through the path and see if it allows you to change directory till you reach res directory. If yes, then inside res directory do ls -l and see if the output shows 636a8fe128.jpg file.
UPDATE
Specify the full path including Rails.root:
def store_dir
"#{Rails.root}/public/uploads/stories/#{model.viewable_id}/res"
end
without #{Rails.root}/public/, path generated was /uploads/stories/533d5b8756617390c0070000/res/636a8fe128.jpg where the first / points to the root directory of the server.
I'm trying to use Fog /Carrierwave/ with Rackspace Cloud Files. I have bunch of uploaded images in my production server. I'm trying to upload these images to Rackspace Cloud Files using below rake task.
desc 'Transfer photos to rackspace'
task :photos => :environment do
photos = Photo.order(created_at: :desc).limit(10)
photos.each do |photo|
if photo.attachment?
photo.attachment.recreate_versions!
photo.save!
else
puts "================================= ATTACHMENT NOT FOUND: ID: #{photo.id}"
end
end
end
But I get following errors:
rake aborted!
undefined method `body' for nil:NilClass
/home/zeck/.rvm/gems/ruby-2.0.0-p247#rails-4-1/gems/carrierwave-0.9.0/lib/carrierwave/storage/fog.rb:227:in `read'
/home/zeck/.rvm/gems/ruby-2.0.0-p247#rails-4-1/gems/carrierwave-0.9.0/lib/carrierwave/uploader/cache.rb:77:in `sanitized_file'
/home/zeck/.rvm/gems/ruby-2.0.0-p247#rails-4-1/gems/carrierwave-0.9.0/lib/carrierwave/uploader/cache.rb:116:in `cache!'
/home/zeck/.rvm/gems/ruby-2.0.0-p247#rails-4-1/gems/carrierwave-0.9.0/lib/carrierwave/uploader/versions.rb:225:in `recreate_versions!'
/home/zeck/code/bee/lib/tasks/bee.rake:9:in `block (4 levels) in <top (required)>'
/home/zeck/.rvm/gems/ruby-2.0.0-p247#rails-4-1/gems/activerecord-4.0.1/lib/active_record/relation/delegation.rb:13:in `each'
/home/zeck/.rvm/gems/ruby-2.0.0-p247#rails-4-1/gems/activerecord-4.0.1/lib/active_record/relation/delegation.rb:13:in `each'
/home/zeck/code/bee/lib/tasks/bee.rake:7:in `block (3 levels) in <top (required)>'
/home/zeck/.rvm/gems/ruby-2.0.0-p247#rails-4-1/bin/ruby_noexec_wrapper:14:in `eval'
/home/zeck/.rvm/gems/ruby-2.0.0-p247#rails-4-1/bin/ruby_noexec_wrapper:14:in `<main>'
It means images not stored in Rackspace Cloud Files. You guys have similar rake task for it? Please share it to me. Or guide me.
Thank you for advice :D
When you change the storage of a CarrierWave uploader from :file to :fog, it loses track of the original uploaded paths of the image files, so methods like recreate_versions! and store! won't be able to find the files to upload.
If you tell CarrierWave the old paths manually, it'll upload them to Cloud Files for you:
desc 'Transfer photos to rackspace'
task :photos => :environment do
photos = Photo.order(created_at: :desc).limit(10)
photos.each do |photo|
if photo.attachment?
# If you've overridden the storage path in the uploader, you'll need to
# use a different path here.
#
# "photo[:attachment]" is used to get the actual attribute value instead
# of the mounted uploader -- the base filename of the attachment file.
path = Rails.root.join('public', 'uploads', photo[:attachment])
unless path.exist?
puts "#{path} doesn't exist. Double check your paths!"
next
end
photo.attachment = path.open
photo.save!
puts "transferred #{photo.id}"
else
puts "================================= ATTACHMENT NOT FOUND: ID: #{photo.id}"
end
end
end
I am attempting to code a very simple way for a user to add html files to my Heroku app. These files would be saved in ./log for rendering later. I have tested my code locally (in both development and production), but when I attempt to upload a file on my Heroku hosted repo I get internal server error 500.
controller upload.rb:
class UploadController < ApplicationController
def index
render :file => 'upload/uploadfile.haml'
end
def uploadFile
file_param = params[:upload][:datafile]
post = DataFile.save(file_param)
render :text => "File has been uploaded successfully"
end
end
model data_file.rb:
class DataFile < ActiveRecord::Base
def self.save(upload)
# Changed Default Destination: [__RAILS_DIR__/log]
name = "./log/" + upload.original_filename
# can haz data directory?
require 'FileUtils'
FileUtils.mkdir_p(File.dirname(name))
File.open(name, "wb") { |f| f.write(upload.read) }
end
end
view uploadfile.haml:
%h1 File Upload
= form_for :upload,:url=>{:action => 'uploadFile'},:html => { :multipart => true } do |f|
%p
%label{:for => "upload_file"} Select File
\:
\#{f.file_field 'datafile'}
= f.submit "Upload"
heroku logs:
2012-08-07T14:13:20+00:00 app[web.1]: Started POST "/uploadFile" for 69.29.117.99 at 2012-08-07 14:13:20 +0000
2012-08-07T14:13:20+00:00 app[web.1]: Processing by UploadController#uploadFile as HTML
2012-08-07T14:13:20+00:00 app[web.1]: Parameters: {"utf8"=>"✓", "authenticity_token"=>"1dAXkMulNR0d8S/l6QC8JnpSDtNBaHoyKJezgnheR10=", "upload"=>{"datafile"=>#>}, "commit"=>"Upload"}
2012-08-07T14:13:20+00:00 app[web.1]: Completed 500 Internal Server Error in 3ms
2012-08-07T14:13:20+00:00 app[web.1]:
2012-08-07T14:13:20+00:00 app[web.1]: LoadError (no such file to load -- FileUtils):
2012-08-07T14:13:20+00:00 app[web.1]: app/models/data_file.rb:7:in save'
2012-08-07T14:13:20+00:00 app[web.1]: app/controllers/upload_controller.rb:8:inuploadFile'
2012-08-07T14:13:20+00:00 app[web.1]:
2012-08-07T14:13:20+00:00 app[web.1]:
2012-08-07T14:13:20+00:00 app[web.1]: cache: [POST /uploadFile] invalidate, pass
heroku: http://upload-example.herokuapp.com/
github: https://github.com/halterj1/upload
Please no trying to convince me to use paperclip or carrierwave, that does not answer my question. Any help would be greatly appreciated, thanks in advance guys!
You should read this article on heroku: https://devcenter.heroku.com/articles/read-only-filesystem
edit:
As stated in article.
Your app is compiled into a slug for fast distribution across the dyno manifold. The filesystem for the slug is read-only, which means you cannot dynamically write to the filesystem for semi-permanent storage. The following types of behaviors are not supported:
Caching pages in the public directory
Saving uploaded assets to local disk (e.g. with attachment_fu or paperclip)
Writing full-text indexes with Ferret
Writing to a filesystem database like SQLite or GDBM
Accessing a git repo for an app like git-wiki
There are two directories that are writeable: ./tmp and ./log (under your application root). If you wish to drop a file temporarily for the duration of the request, you can write to a filename like #{RAILS_ROOT}/tmp/myfile_#{Process.pid}. There is no guarantee that this file will be there on subsequent requests (although it might be), so this should not be used for any kind of permanent storage.
I'm running Cedar stack on Heroku, rails 3.1.3.
using:
http://trevorturk.com/2009/11/05/no-www-rack-middleware/
When I push to Heroku, I get:
app[web.1]: => Rails 3.1.3 application starting in production on http://0.0.0.0:15548
app[web.1]: => Call with -d to detach
app[web.1]: => Ctrl-C to shutdown server
app[web.1]: Exiting
app[web.1]: /app/config/environment.rb:7:in `<top (required)>': uninitialized constant Rails::Initializer (NameError)
Anyone have any ideas whats going on?
Here's my environment.rb (AppName = proper name for my app. i.e. thats not the issue)
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
AppName::Application.initialize!
Rails::Initializer.run do |config|
config.middleware.use "NoWWW" if RAILS_ENV == 'production'
end
lib/no_www.rb:
class NoWWW
STARTS_WITH_WWW = /^www\./i
def initialize(app)
#app = app
end
def call(env)
if env['HTTP_HOST'] =~ STARTS_WITH_WWW
[301, { 'Location' => Rack::Request.new(env).url.sub(/www\./i, '') }, ['Redirecting...']]
else
#app.call(env)
end
end
end
I had the same problem using Rails 3.1. I ended up using this post. It appears to be more involved than other solutions but there are really only two steps.
Make sure to change yoursite.com in the code. I overlooked this and had to rush another deployment after the fix.
The error you're receiving is telling you that you're referencing an old version of Rails. Rails 3.1 initializes quite a bit differently than it did 2 years ago when that article was written. In particular, the problem is with the now deprecated Rails::Initializer in this block:
Rails::Initializer.run do |config|
config.middleware.use "NoWWW" if RAILS_ENV == 'production'
end
You might have more luck with rack-rewrite. Regardless, check out the official Rails documentation for a good breakdown of current configuration and initialization.
It looks like your middleware file is not being loaded. Place your middleware class, no_www.rb in app/middleware. This way it will be auto-loaded by Rails. Then add your config statement to application.rb, near the end.
...
# Configure Rack middleware
config.middleware.use 'NoWWW'
end
end
I had this issue; I know it's a late answer but I wanted to add this for whoever needed it.
Make sure that your OmniauthCallbacksController IS IN A users FOLDER. "app/controllers/users/omniauth_callbacks_controller.rb"