Paperclip File Not Found Error - ruby-on-rails

I just switched from a paperclip rails plugin to a paperclip gem. The project is a rails 2.3 application and I am using paperclip 2.7.2 gem.
I am getting the following odd error:
identify: unable to open image `file': No such file or directory # error/blob.c/OpenBlob/2617.
identify: no decode delegate for this image format `file' # error/constitute.c/ReadImage/544.
Seems like paperclip is looking for a file called 'file' but I am not sure why. I didn't change any of the code we had before. It used to work, all I did was upgrade to a newer version and use a gem over a plugin.
Any ideas?
Update
It to be a bug in paper clip where it does not parse the content of the command properly. I dug deep into the paperclip source to find:
def run(cmd, arguments = "", local_options = {})
if options[:image_magick_path]
Paperclip.log("[DEPRECATION] :image_magick_path is deprecated and will be removed. Use :command_path instead")
end
command_path = options[:command_path] || options[:image_magick_path]
Cocaine::CommandLine.path = [Cocaine::CommandLine.path, command_path].flatten.compact.uniq
local_options = local_options.merge(:logger => logger) if logging? && (options[:log_command] || local_options[:log_command])
Cocaine::CommandLine.new(cmd, arguments, local_options).run
end
and
# Uses ImageMagick to determing the dimensions of a file, passed in as either a
# File or path.
# NOTE: (race cond) Do not reassign the 'file' variable inside this method as it is likely to be
# a Tempfile object, which would be eligible for file deletion when no longer referenced.
def self.from_file file
file_path = file.respond_to?(:path) ? file.path : file
raise(Paperclip::NotIdentifiedByImageMagickError.new("Cannot find the geometry of a file with a blank name")) if file_path.blank?
geometry = begin
Paperclip.run("identify", "-format %wx%h :file", :file => "#{file_path}[0]")
rescue Cocaine::ExitStatusError
""
rescue Cocaine::CommandNotFoundError => e
raise Paperclip::CommandNotFoundError.new("Could not run the `identify` command. Please install ImageMagick.")
end
parse(geometry) ||
raise(NotIdentifiedByImageMagickError.new("#{file_path} is not recognized by the 'identify' command."))
end
The Paperclip.run command fails to replace the :file placeholder for some reason. I verified this by running the following on the commandline:
identify :file
Monkey patching the replacement by hand yields other errors where a similar thing happens.

Ok I managed to solve it.
It was a problem with Cocaine. Seems paperclip has a dependency on cocaine that only says it must be Cociane > 0.02. The latest version of Cocaine 0.4.2 (at the time of this writing) has a new API which is not backward compatible. You need to downgrade to Cocaine 0.3.2.
Simply put this in your Gemfile before paperclip:
gem "cocaine", "0.3.2"

Related

Sitemap_generator fails to upload

I've followed the instructions on a couple of pages for getting a sitemap to generate and be uploaded to my S3 Bucket. The sitemap is generating, but not uploading.
I'm using carrierwave for the upload, which is working fine for image uploads.
The key file seems to be config/sitemap.rb. Here's mine:
require 'rubygems'
require 'sitemap_generator'
# Set the host name for URL creation
SitemapGenerator::Sitemap.default_host = "https://www.driverhunt.com"
# pick a place safe to write the files
SitemapGenerator::Sitemap.public_path = 'tmp/'
# store on S3 using #Fog# Carrierwave
SitemapGenerator::Sitemap.adapter = SitemapGenerator::WaveAdapter.new
# SitemapGenerator::Sitemap.adapter = SitemapGenerator::S3Adapter.new
# This is a different problem to the one in the question, but using this second adaptor gives the error: "...lib/fog/storage.rb:27:in `new': is not a recognized storage provider (ArgumentError)"
# inform the map cross-linking where to find the other maps
SitemapGenerator::Sitemap.sitemaps_host = "http://#{ENV['S3_BUCKET']}.s3.amazonaws.com/"
# pick a namespace within your bucket to organize your maps
SitemapGenerator::Sitemap.sitemaps_path = 'sitemaps/'
SitemapGenerator::Sitemap.create do
add '/home', :changefreq => 'daily', :priority => 0.9
# add '/contact_us', :changefreq => 'weekly'
end
# SitemapGenerator::Sitemap.ping_search_engines # Not needed if you use the rake tasks
What's going on? How do I debug a carrierwave upload?
I will answer the question as your comment for the S3Adapter brought me to this topic while I was googling the not recognized provider. If you turn back on the comment using the S3Adapter and do the following you will get it working.
If you do not specify any fog ENV VARS for the fog-aws gem you will get the error:
ArgumentError: is not a recognized provider
by using as an adapter the SitemapGenerator::S3Adapter.new
The setup you have got above is perfectly fine, just use the S3Adapter.new instead of the WaveAdapter!
The error you are getting (and I was getting as well) is due to the fact that SitemapGenerator::S3Adapter uses fog-aws and in order to make it run by default you should have the following ENV VARS:
ENV['AWS_ACCESS_KEY_ID'] = XXX
ENV['AWS_SECRET_ACCESS_KEY'] = XXX
ENV['FOG_PROVIDER'] = AWS
ENV['FOG_DIRECTORY'] = your-bucket-name
ENV['FOG_REGION'] = your-bucket-region (ex: us-west-2)
If you are missing even one of the following you will get the error:
ArgumentError: is not a recognized provider
Alternativelly, if you want to avoid using ENV VARS for some reason you should specify the values when you initialize your adapter as follows:
SitemapGenerator::Sitemap.adapter = SitemapGenerator::S3Adapter.new(fog_provider: 'AWS',
aws_access_key_id: 'your-access-key-id',
aws_secret_access_key: 'your-access-key',
fog_directory: 'your-bucket',
fog_region: 'your-aws-region')
However using just the above ENV VARS you will be fine and get your sitemap up and running. This setup was tested with sitemap_generator version: 5.1.0
For your question:
The Image uploading works as it does not require the exact same configuration as the WaveAdapter. I am guessing that your carrierwave.rb file is missing the following:
config.cache_dir = "#{Rails.root}/tmp/"
config.permissions = 0666
The complete configuration for the carrierwave initializer can be found here:
Generate Sitemaps on read only filesystems like Heroku (check if you are missing something or use the other adapter)
However, I believe that your problem has to do with missing ENV VARS from the production environment.

ExecJS::RuntimeError running the Getting Started with Rails blog on cygwin (windows7)

I am trying to run the Getting Started with Rails blog on cygwin (windows7). I get the following error message:
ExecJS:: RuntimeError in Welcome#index
module.js:340
throw err;
^
Error: Cannot find module 'C:\tmp\execjs20130903-50672-1vn7gqc.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
(in /usr/lib/ruby/gems/1.9.1/gems/turbolinks-1.3.0/lib/assets/javascripts /turbolinks.js.coffee)
node is installed.
This is after
$rails generate controller welcome index
$rails s
I am running Rails 4.0 on cygwin
Any ideas why this might be happening?
Thanks
umbregachoong
I encountered this error and it had to do with the path to the temp file being wrong. I was able to fix it by changing the following two files in \gems\[ruby version]\gems\execjs-2.0.2\lib\execjs.
(Possibly found in \usr\lib\ruby\, but that depends on how your Ruby is installed. I'm using RVM so mine is different.)
external_runtime.rb
compile_to_tempfile(source) do |file|
extract_result(#runtime.send(:exec_runtime, file.path))
end
end
should change to
compile_to_tempfile(source) do |file|
filepath = file.path
if ExecJS.cygwin? && #runtime.name == "JScript"
IO.popen("cygpath -m " + file.path) { |f| filepath = f.read }
filepath = filepath.gsub("\n","")
end
extract_result(#runtime.send(:exec_runtime, filepath))
end
end
module.rb
Add this right before the last two ends.
def cygwin?
#cygwin ||= RbConfig::CONFIG["host_os"] =~ /cygwin/
end
After this restart your Rails server and with any luck it should work.
Source: https://github.com/sstephenson/execjs/issues/78
don't do anything just go to application/assets/javascript/application.js and remove
'//'
from
//=require turbolinks
to
=require turbolinks
that will basically uncomment the line from the file
This step made my work done when i faced similar error in Default rails server going through RAILS Tutorial. I am using Windows 10 PC and this solved my case

Change a RDoc template for generating Rails app documentation

I have just added some documentation to my Rails 3.2.13 app. I can generate the documentation just fine (running RDoc 3.12.2) by using a rake task:
# lib/tasks/documentation.rake
Rake::Task["doc:app"].clear
Rake::Task["doc/app"].clear
Rake::Task["doc/app/index.html"].clear
namespace :doc do
RDoc::Task.new('app') do |rdoc|
rdoc.rdoc_dir = 'doc/app'
rdoc.generator = 'hanna'
rdoc.title = 'Stoffi Web App Documentation'
rdoc.main = 'doc/Overview'
rdoc.options << '--charset' << 'utf-8'
rdoc.rdoc_files.include('app/**/*.rb')
rdoc.rdoc_files.include('doc/*')
end
end
...and then running rake doc:app. But I really don't like the default look of the Hanna template. Is there a way to edit the CSS, perhaps by providing my own CSS file which will override the default one used in Hanna?
Thanks!
First of all find where your templates are located:
⮀ RDPATH=$(dirname $(gem which rdoc))
# ⇒ /home/am/.rvm/rubies/ruby-head/lib/ruby/2.1.0
Now copy the default template from there to the desired location (change /tmp to your project directory or like):
⮀ cp -r $RDPATH/rdoc/generator/template/darkfish /tmp/myniftytemplate
And, finally, let’s teach the rdoc:
class RDoc::Options
def template_dir_for template
"/tmp/#{template}"
end
end
RDoc::Task.new('app') do |rdoc|
rdoc.template = 'myniftytemplate'
…
end
That’s it. Hope it helps.

Setting wkhtmltopdf path in rails

I'm having this error when I try to use wicked_pdf:
irb: No such file or directory - which wkhtmltopdf
NoMethodError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.chomp
from C:/Ruby187/lib/ruby/gems/1.8/gems/wicked_pdf-0.7.2/lib/wicked_pdf.r
b:19:in `initialize'
from (irb):2:in `new'
from (irb):2
from :0
I think the path to wkhtmltopdf should be configured. But how?
Provide sth like
:wkhtmltopdf => '/usr/local/bin/wkhtmltopdf', # path to binary
with your render call and make sure, that a wkhtmltopdf binary resides at that path.
(You can get wkhtmltopdf from google code in case you have not installed it yet)
According to the sources, simply add it to your $PATH (the folder containing the binary).
Wkhtmltopdf project can't be found on Google Code any longer. It was moved to http://wkhtmltopdf.org/downloads.html
In my case, I just had to download the OSX binary and it was automatically added to my PATH variable.
Create an initializer: config/initializers/wicked_pdf.rb:
WickedPdf.config = {
:exe_path => "#{ENV['GEM_HOME']}/bin/wkhtmltopdf"
}
:exe_path should hold the path to the wkhtmltopdf binary. For my instance, I used the gem:
gem "wkhtmltopdf-binary"
Which stored the binary in my GEM_HOME, so I could reference a path that wasnt hard coded.

Paperclip + ImageMagick on Windows 7: Image display fails when I add styles to attached_file in model

I'm working with Ruby on rails 2.3.8, NetBeans IDE.
I've installed paperclip and I could show/save images successfully. Now, I've installed ImageMagick-6.6.2-4-Q16(for windows 7, 64bits).
Until that moment, my model looked like this(and worked fine):
has_attached_file :photo
Now, after installing ImageMagick, when I add the :style line it fails:
has_attached_file :photo,
:styles => {:thumb => "100x100#", :small => "150x150>", :large => "400x400>" }
and it throws the following error message when I try to upload an image:
TypeError in ProfilesController#update
backtrace must be Array of String
The only thing I'm doing in the update action of that controller is the following:
#profile.update_attributes(params[:profile])
#profile.update_attribute(:photo, params[:profile][:photo])
I've also installed miniMagick gem(because I read somewhere I had to do it).
What am I missing?
I'll show you what it worked for us:
There is a name conflict with "convert" command. Paperclips tries to run "convert" as is, but this command is already included in the Windows installations as a filesystem converter (FAT to NTFS or something).
If you try to run "convert" from command line, probably will run the mentioned converter instead of imagemagick's "convert"
It depends on the PATH environment variable.
If we set imagemagick's path FIRST in the PATH variable it will resolve this path first, so the windows' command won't be executed.
In order to fix it on our rails application, we added
...
if Sys::Uname.sysname == "Linux"
...
else
....
ENV['PATH'] = Paperclip.options[:command_path] + ";" + ENV['PATH']
end
...
on production.rb
Try to download the paperclip version 2.3.1.1, higher versions failed to me.

Resources