Rails CarrierWave versions are not created for some reason - ruby-on-rails-3

I have the following file uploader
class ItemImageUploader < CarrierWave::Uploader::Base
include ::CarrierWave::Backgrounder::Delay
include CarrierWave::MiniMagick
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url
asset_path("fallback/" + [version_name, "image.png"].compact.join('_'))
end
def cache_dir
"uploads/tmp"
end
# Create different versions of your uploaded files:
version :thumb do
process :resize_to_fill => [80,80]
end
def extension_white_list
%w(jpg jpeg gif png)
end
Picture class
class Picture < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
mount_uploader :image, ItemImageUploader
process_in_background :image
validates_presence_of :image
def copy
Picture.new(:image => self.image)
end
And the following config in carrier_wave.rb initialization file:
CarrierWave.configure do |config|
config.enable_processing = true
#config.permissions = 0666
#config.directory_permissions = 0777
config.storage = :file
end
Images uploaded properly, but no thumbnails created, no errors occurred. Please advise.

Just hit my head against this one myself. As the comments suggest, using carrierwave_backgrounder causes this problem. You can see it in their documentation:
process_in_background - This stores the original file with no processing/versioning.
Doesn't help with a solution, but I thought I'd verify the problem.

I struggled with this for a couple of hours today, because I had the same issue. Uploading of the original version would work, but it was not resized. It was working locally, but not on my production machine. Hope someone with the same issues stumbles on this answer and saves some time.
Turned out that my version of ImageMagick was not build with the correct delegates, this was the output of $ convert -version:
Version: ImageMagick 6.9.1-10 Q16 x86_64 2015-08-01 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC OpenMP
Delegates (built-in): zlib
I used this answer to install the delegates and build a fresh version of ImageMagick: ImageMagick missing decode delegates
After that, pulled up the ImageMagick info with $ convert -version:
Version: ImageMagick 6.9.1-10 Q16 x86_64 2015-08-01 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC OpenMP
Delegates (built-in): bzlib djvu fontconfig freetype gvc jbig jng jpeg lcms lqr lzma openexr png tiff wmf x xml zlib
And now I'm rollin'!

Related

Nil param in conditional version Carrierwave + Cloudinary Rails 5

I am having uploading problem using carrierwave and cloudinary on heroku platform in production. Specifically in conditional versioning. Uploading occurs seamlessly to cloudinary, but in generation the version the error occurs.
See my very simple uploader class bellow:
class MediaAlbumUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
if Rails.env.production?
include Cloudinary::CarrierWave
# storage :fog
else
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
version :thumb, if: :image? do
process resize_and_pad: [450, 300]
end
def extension_whitelist
%w(jpg jpeg gif png)
end
protected
def image?(new_file)
new_file.content_type.start_with? 'image'
end
end
In the verification image? the param new_file is nil, look the error:
NoMethodError (undefined method `content_type' for nil:NilClass)
The has extract with heroku logs -t
Remember, uploading to cloudinary occurs without problems so the new_file should not be null.

MiniMagick and Carrierwave not resizing

Using Rails 3.2.12 and Ruby 1.9.2
I simply want to use carrierwave/minimagick as so:
In my Gemfile
gem 'carrierwave'
gem 'mini_magick'
In my uploader
require 'carrierwave/processing/mini_magick'
class AvatarUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url
# # For Rails 3.1+ asset pipeline compatibility:
ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
end
process :resize_to_fit => [800, 800]
version :thumb do
process :resize_to_fit => [200,200]
end
version :mini do
process :resize_to_fit => [50,50]
end
version :medium do
process :resize_to_fit => [250,250]
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
The problem is that the thumb, mini and medium versions are saving itself properly but not resizing (the size is the same for all versions).
anyone experiencing something similar?
Ok just figured it out.... after 4 hours of debugging!
I was using ruby 1.9.2 that was not compatible with the resizing of minimagick
Once I upgraded to ruby 1.9.3 everything went back to normal and resizing worked perfectly.
This should be clearly documented.

Why does PDFKit/wkhtmltopdf hang but renders PDF as expected when Rails app is killed?

Background
After reading around it seemed to me that Prawn is out and wkhtmltopdf is in. It also seems like the PDFKit and wicked_pdf gems for Rails are the new cool. So I found a screencast by Ryan on how to use PDFKit. I installed everything, tested wkhtmltopdf on the CLI with no problems, fiddled around with Rails settingsto run multiple processes so the asset pipeline works, and all seemed good, except I'm still stuck at the very end of the process (actually getting the PDF response from the server).
Question
When I request a .pdf version of my view (I'm using the PDFKit Middleware option) my browser just sits there waiting for a response, but as soon as I kill the Rails process the PDF I expected to get only then pops up in my browser window. What gives?
What I'm Using
OS: OSX 10.8.1
Rails: 3.2.8
Ruby: 1.9.3
wkhtmltopdf: 0.11.0_rc1 (although when I run wkhtmltopdf -V it says 0.10.0_rc2)
qt: 4.8.2
What I've Done
used the PDFKit middleware by loading config.middleware.use "PDFKit::Middleware" in my application.rb file.
included gem 'pdfkit' in my Gemfile and installed it with Bundler
set the .pdf mime-type in my mime_types.rb initializer with Mime::Type.register_alias "application/pdf", :pdf
added config.threadsafe! to config/environments/development.rb for multiple threads so asset pipeline doesn't conflict with PDF engine
tested wkhtmltopdf http://www.google.com google.pdf and it generated a PDF of the Google homepage as expected
tried swapping PDFKit for wicked_pdf and encountered the same problem (hanging, but when Rails process is killed the PDF renders as expected)
What it Looks Like
This is the regular html page rendered by Rails (I've blurred the client details):
This is the CLI output by Rails when I try to navigate to localhost:3000/some/path.pdf. (the app hangs while waiting for a response):
When I finally kill the Rails process with ctrl-c the PDF finally shows up in the browser as I expected to see it (CSS and HTML rendered properly, so assets seem to load fine):
Conclusions So Far
Swapping PDFKit for wicked_pdf and getting the same results seems to make me think the problem isn't with those libraries, but something to do with my development environment. But wkhtmltopdf runs fine off the command line, so that makes me think that it and QT are doing their job. The problem must be in Rails. Maybe I'm not configuring something properly?
Plea for Help
How do I determine what exactly the problem is and how do I fix it?
I'll love you if you can help me <3
Update
I've also tried using an alternative method of rendering the PDF (with .to_pdf) without the middleware option as follows (doing this I commented out config.middleware.use "PDFKit::Middleware" from my application.rb file):
respond_to do |format|
format.html
format.pdf do
html = '<html><body>This is a test.</body></html>'
#pdf = PDFKit.new(html)
send_data #pdf.to_pdf,
:filename => 'whatever.pdf',
:type => 'application/pdf',
:disposition => 'attachment'
end
end
The problem is with wkhtmltopdf itself, specifically, any version after 0.9.9. wkhtmltopdf hangs when run directly from the command-line.
Steps to correct:
brew uninstall wkhtmltopdf
cd /usr/local/Library/Formula/
git checkout 6e2d550 /usr/local/Library/Formula/wkhtmltopdf.rb
brew install wkhtmltopdf
Then verify the correct version is installed wkhtmltopdf --version which should yield wkhtmltopdf 0.9.9
Citations:
https://github.com/mileszs/wicked_pdf/issues/110
http://wearepandr.com/blog/article/homebrew-and-installing-old-package-versions#blog_nav
Try the last version. The easy way install on MacOS:
brew install Caskroom/cask/wkhtmltopdf
The fix by scarver2 worked for me as advertised. But I needed a more recent version of wkhtmltopdf. Since the homebrew version still seems outdated (it still hangs on the command line), and since there isn't a recent precompiled binary available, I used the os x build script to compile one myself:
$ git clone git#github.com:wkhtmltopdf/wkhtmltopdf.git
$ cd wkhtmltopdf
$ ./build_osx.sh # i'm running 10.9.2
$ bin/wkhtmltopdf --version
Name:
wkhtmltopdf 0.12.1-72a9f2066fe9ffd162dec007a4d9b6a5cd63b670
$ curl www.example.com | bin/wkhtmltopdf - test.pdf # render pdf of example.com
$ open test.pdf # to confirm pdf
I'm using pdfkit 0.6.2 with Rails 3.2.17. I put the binary in /vendor and, in a pdfkit initializer, pointed to it with config.wkhtmltopdf. So far, so good.
I got the same issue. It works when I added: 'config.threadsafe!' in application.rb as the answer in the stack. Hope this help.
Mine was also hanging and opening the wkhtmltopdf icon in the dock.
I actually found the problem for me was I only had 1 unicorn worker process running. Once I added more than 1 it worked fine.
I'm running wkhtmltopdf 0.9.9 on OS X with pdfkit 0.6.2
Exact same symptoms but using WickedPdf currently. At this point, I believe the issue lies with wkhtmltopdf as well.
Unfortunately, neither of the recommendations I've been able to find in Stack/Google worked for me. Instead, I needed to combine several suggestions, including some found in this post.
Solution was:
brew uninstall wkhtmltopdf
find and delete any copies of wkhtmltopdf in /usr/bin
comment WickedPdf.config line in config/initializers
add config.threadsafe! to development.rb
remove middleware and allow 'show' action of main controller to handle pdf requests (may not be necessary)
add wkhtmltopdf-binary to gemfile
bundle
restart server
You may also need to add Mime::Type.register_alias "application/pdf", :pdf to config/initializers/mime_types.rb (for me, this causes 'warning: already initialized constant PDF')
My set-up was: Mac OSX Mountain Lion with Rails 3.2.1, Webrick, Postgres and wkhtmltopdf-binary (0.9.9.1).
Try editing config/initializer/pdfkit.rb in the following way:
PDFKit.configure do |config|
config.wkhtmltopdf = Rails.root.join('bin', 'wkhtmltopdf-i386').to_s
config.default_options = {
:encoding=>"UTF-8",
:page_size=>"A4",
:margin_top=>"0.25in",
:margin_right=>"0.1in",
:margin_bottom=>"0.25in",
:margin_left=>"0.1in",
:disable_smart_shrinking=> false
}
end

rails3 with compass-rails

I'm trying to set up a rails 3.2 app with the new compass-rails gem
https://github.com/Compass/compass-rails
I want to compile the files in the assets folder with the compass watch command—as I want to deploy to heroku, but so far i'm unsuccessful.
The compass-rails gem page states:
Developing with the Compass watcher
When using the Compass watcher to update your stylesheets, your stylesheets are recompiled as soon as you save your Sass files. In this mode, compiled stylesheets will be written to your project's public folder and therefore will be served directly by your project's web server -- superceding the normal rails compilation.
In this mode, rails 3.0 or earlier users will experience a slight speed up by disabling the Sass::Plugin like so:
config.after_initialize do
Sass::Plugin.options[:never_update] = true
end
But i don't get where to put this config options
Any idea?
==EDIT==
I tried to add the config.after_initialize block in my application.rb
require File.expand_path('../boot', __FILE__)
# require 'rails/all'
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
# require "sprockets/railtie"
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module Salsacaribecouk
class Application < Rails::Application
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
# Enable the asset pipeline
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
config.after_initialize do
Sass::Plugin.options[:never_update] = true
end
end
end
but when i run the rails server I get an error message:
block in <class:Application>': uninitialized constant Sass::Plugin (NameError)
The config.after_initialize should be put in config\application.rb if you want it to be executed in all environments, or in config\environments\[development|test|production].rb to executed in a specific environment.

how to config ruport in the environment.rb

i have installed ruport gem along with its ruport-utils packages and act_as_reportable gem too....
e
"Ash::Application.initialize!
Rails::Initializer.run do |config|
config.after_initialize do
require "ruport"
end
end"
when executing this in the environment.rb file... it shows this "
c:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.0.9/lib/rails/application.rb:63:in `inherited': You cannot have more than one Rails::Application (RuntimeError)"
what am i doing wrong.... can somebody help me with properly configuration method of ruport.
ok found wht i was doing wrong... apparently the source i was referring for was for earlier verion... with the new one just put this " config.autoload_paths += %W(::RAILS.root.to_s/app/reports) " in the config/environments/development.rb file , also specify yur gem in the gemfile and do a bundle insatll. and and also for generating textile file ie for rendering the report in html format you would be required to install RedCloth gem version 3.0.3(thats what i have installed in my system.)
The most reliable way to do so, and be sure that Ruport is loaded at the proper time, is to add the code to require Ruport to a config.after_initialize block. The relevant section of the configuration file is shown below.
Rails::Initializer.run do |config|
config.after_initialize do
require "ruport"
end
end