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.
Related
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.
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'!
GETTING THIS ERROR WHEN UPLOADING A FILE :
LoadError (no such file to load -- aws-sdk (You may need to install the aws-sdk gem)):
app/controllers/uploaded_files_controller.rb:19:in `create'
I am using Mongo and Paperclip. I can upload files fine without using s3. However, our production server is on Heroku and so I have to use Amazon to store the files.
I've read other Stack Overflow posts about this but none address my specific issue.
I have restarted my server several times. that's not it.
I am indeed requiring the Amazon gem in my Gemfile
I have done a bundle install after putting the amazon gem in ( I know its obvious, but still I had to state this )
I am NOT using ImageMagick. These uploads are simple text file uploads.
I know that my Amazon bucket name and auth stuff is correct because I use this app to connect to other Amazon resources in a different capacity.
Can anyone help with this ? Here is my code:
class UploadedFile
include Mongoid::Document
include Mongoid::Paperclip
require "aws/s3"
has_mongoid_attached_file :file,
:storage => :s3,
:bucket_name => 'my-uploads',
:path => ':attachment/:id/:style.:extension',
:s3_credentials => File.join(Rails.root, 'config', 'amazon_s3.yml')
end
OK, I've found the answer: The gem needs to be updated.
Paperclip now requires Amazon SDK gem instead of the s3 gem.
gem 'aws-s3', :require => "aws/s3"
should be instead
gem 'aws-sdk', :require => "aws-sdk"
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.
I have a resque worker which works great but is just too slow. The main reason for this is I'm using activerecord and having to load the entire environment which takes at least 10-20 seconds just to load up (I don't keep a running worker at all times as I'm using Heroku and pay for the time the worker runs). I'm using a resque worker to grab & parse data from an external website and then dumping the data into my database.
My question is whether I should rewrite the method to not use Rails and instead use DataMapper? Or something else which would load faster than activerecord.
Or If I should extract the code (using ActiveRecord) which figures out what to do with the external data and move it out of the worker and back into the app?
Hope that makes sense.
I have the same problem.
you could setup your environment on the rake resque:setup rake task
I tried this. assuming my rake resque task is on lib/tasks/resque.rake
require "resque/tasks"
task "resque:setup" do
root_path = "#{File.dirname(__FILE__)}/../.."
db_config = YAML::load(File.open(File.join(root_path,'config','database.yml')))["development"]
ActiveRecord::Base.establish_connection(db_config)
require "#{root_path}/app/workers/photo_downloader.rb" #workers
#Dir.glob("#{root_path}/app/models/*").each { |r| puts r; require r } #require all model
require "#{root_path}/app/models/photo.rb" # require model individually
end
I haven't completely success beacuse I use the Paperclip gem which require rails environment
Rails’ bootstrap is really slow; it is intended to be kept running, until certain time for restart (to eliminate some memory leaks there most likely is, any software is not bug-free), and is not intended to be used as a site that is launched for one request and then shut down.
That kind of usage more resembles a script. If you need to launch it with browser, you can easily use something like Erubis to write the page and use ActiveRecord in the script (I think it was useable outside of rails) or similar abstraction layer. Myself, for small tasks, I just use Mysql2.
Use bundler to get active_record and other gem to you without rails application .
require 'rubygems'
require 'logger'
require 'active_record'
require 'bundler'
require "active_support"
require "spreadsheet"
require 'net/ping'
require 'net/http'
Bundler.setup
Bundler.require(:default) if defined?(Bundler)
$config_logger = Logger.new("./log/dev.log")
class Dbconnect
def initialize
#settings = YAML.load_file('./config/database.yml')["development"]
#adapter = #settings["adapter"] if #settings["adapter"]
#database = #settings["database"] if #settings["database"]
#pool = #settings["pool"] if #settings["pool"]
#timeout = #settings["timeout"] if #settings["timeout"]
end
def connect_to_db
ActiveRecord::Base.establish_connection(
:adapter => #adapter,
:database => #database,
:reconnect => #reconnect,
:pool => #pool,
:timeout => #timeout)
$config_logger.info "\n db Connected: to => #{#database} "
end
end
end
}
Example Gemfile :
source "http://rubygems.org"
gem 'mail'
gem "escape_utils"
gem 'json',:require => "json"
gem 'json_pure'
gem 'resque'
gem 'resque-scheduler'
gem 'redis-namespace'
gem 'resque-status'
gem 'rake'
gem 'em-udns'
gem 'sqlite3'
gem 'spreadsheet'
gem 'activerecord', '3.2.1', :require => "active_record"
gem 'net-scp', :require => 'net/scp'
gem 'net-sftp', :require => 'net/sftp'
gem 'net-ssh', :require => 'net/ssh'
gem 'dir'
gem 'amatch'
gem 'haml'
gem 'net-ping'
gem install bundler
rest of the thing : bundle install .