Paperclip looking for file locally for reprocessing when using S3 - ruby-on-rails-3

I am using paperclip to upload files to s3 storage. Once the file is uploaded I am trying to crop it using Jcrop. When the logo.reprocess! runs It tries to look for the file locally rather than on s3 and gives me a No such file or directory error. Here is relevant code
has_attached_file :logo,
styles: { thumb: "145x75#", large: "500x500" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:processors => [:cropper]
def cropping_logo?
!logo_crop_x.blank? && !logo_crop_y.blank? && !logo_crop_w.blank? && !logo_crop_h.blank?
end
def logo_geometry(style = :original)
#geometry ||= {}
path = (logo.options[:storage]==:s3) ? logo.url(style) : logo.path(style)
#geometry[style] ||= Paperclip::Geometry.from_file(path)
end
def reprocess_logo
logo.reprocess!
end
module Paperclip
class Cropper < Thumbnail
def transformation_command
if crop_command
crop_command + super.sub(/ -crop \S+/, '')
else
super
end
end
def crop_command
target = #attachment.instance
if target.cropping_logo?
" -crop '#{target.logo_crop_w.to_i}x#{target.logo_crop_h.to_i}+#{target.logo_crop_x.to_i}+#{target.logo_crop_y.to_i}'"
end
end
end
end
Here is some stacktrace for help
/usr/local/ruby/lib/ruby/1.9.1/fileutils.rb:1423:in `stat'
/usr/local/ruby/lib/ruby/1.9.1/fileutils.rb:1423:in `block in fu_each_src_dest'
/usr/local/ruby/lib/ruby/1.9.1/fileutils.rb:1439:in `fu_each_src_dest0'
/usr/local/ruby/lib/ruby/1.9.1/fileutils.rb:1421:in `fu_each_src_dest'
/usr/local/ruby/lib/ruby/1.9.1/fileutils.rb:391:in `cp'
paperclip (3.0.2) lib/paperclip/io_adapters/attachment_adapter.rb:53:in `copy_to_tempfile'
paperclip (3.0.2) lib/paperclip/io_adapters/attachment_adapter.rb:44:in `cache_current_values'
paperclip (3.0.2) lib/paperclip/io_adapters/attachment_adapter.rb:6:in `initialize'
paperclip (3.0.2) lib/paperclip/io_adapters/registry.rb:29:in `new'
paperclip (3.0.2) lib/paperclip/io_adapters/registry.rb:29:in `for'
paperclip (3.0.2) lib/paperclip/attachment.rb:91:in `assign'
paperclip (3.0.2) lib/paperclip/attachment.rb:279:in `reprocess!'

I have the same issue. I think it's related to the Paperclip v3 code.
I specified in my Gemfile to use the older version.
# Gemfile
gem "paperclip", "~> 2.7.0"
And cropping works. I'm not sure if that's a good solution for you, but it works for me for the time being.

Related

how can I send an email from an IronWorker for a rails3 app?

I followed all the docs for IronWorkerNG::Client.new and rails:
https://github.com/iron-io/iron_worker_rails_example
My .worker file is doing:
runtime 'ruby'
name 'CompanyList'
merge_gem 'activerecord'
merge_gem 'actionmailer'
merge_gem 'pg'
merge_gem 'aws-s3'
dir '../app/models'
file '../app/mailers/result_mailer.rb'
file "../app/views/result_mailer/email.html.erb", "result_mailer"
exec 'company_list.rb'
remote
and my ruby file does:
require 'action_mailer'
require 'active_record'
require 'aws/s3'
require 'pg'
require 'models/company_list.rb'
require 'result_mailer.rb'
puts "Starting CompanyList worker at #{Time.now}"
ActiveRecord::Base.establish_connection(params['database'])
mailer_config = params['mailer'].inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo }
#ActionMailer::Base.prepend_view_path('.')
ActionMailer::Base.view_paths = ['.']
ActionMailer::Base.smtp_settings = mailer_config
ActionMailer::Base.delivery_method = :smtp
But everytime I get this error:
/task/__gems__/gems/actionmailer-2.3.18/lib/action_mailer/base.rb:433:
in `method_missing': undefined method `email' for ResultMailer:Class
(NoMethodError)
from /task/models/company_list.rb:16:in `process_file'
from /task/company_list.rb:18:in `<top (required)>'
from /usr/local/lib/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/local/lib/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from __runner__.rb:213:in `<main>'
and ResultMailer DOES have an email method:
class ResultMailer < ActionMailer::Base
def email
to = 'andrew#testdomain.com'
mail :from => 'test#testdomain.com', :to => to, :subject => "Test"
end
end
any idea why action_mailer base line 433 is throwing that error? I can call ResultMailer.email.deliver from the console on my local system just fine. This error is on production with heroku and iron.io.
ah, sometimes writing out the question on stackoverflow makes you see the problem. I needed:
merge_gem "activerecord", "=3.2.8"
merge_gem 'actionmailer', '=3.2.8'
without the 3.2.8 version it was using an old old version of action mailer.

CarrierWave not working with Fog -> ArgumentError ( is not a recognized storage provider): app/controllers/notes_controller.rb:11:in `create'

I followed the railscasts about using carrierwave with fog with aws s3 to the T but it doesnt seem to work.
The Gemfile is as follows:
source 'https://rubygems.org'
ruby '1.9.3'
gem 'rails', '3.2.13'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'pg'
gem 'pg_search'
gem 'devise'
gem "therubyracer"
gem "less-rails" #Sprockets (what Rails 3.1 uses for its asset pipeline) supports LESS
gem "twitter-bootstrap-rails", :git => 'git://github.com/seyhunak/twitter-bootstrap-rails.git'
gem "simple_form"
gem 'aws-sdk', '~> 1.8.1.2'
gem 'activerecord-reputation-system', require: 'reputation_system'
gem 'bootstrap-will_paginate'
gem 'carrierwave'
gem "rmagick"
gem "fog", "~> 1.3.1"
gem 'carrierwave-aws'
config/initializers/carrierwave.rb
require 'carrierwave'
CarrierWave.configure do |config|
config.fog_credentials = {
provider: "AWS",
aws_access_key_id: ENV["xxx"],
aws_secret_access_key: ENV["yyy"],
region: ENV['ap-southeast-1']
}
config.cache_dir = "#{Rails.root}/tmp/uploads"
config.fog_directory = ENV["ABC"]
end
The uploader:
class ImageUploader < CarrierWave::Uploader::Base
require 'carrierwave/processing/mime_types'
before :store, :remember_cache_id
after :store, :delete_tmp_dir
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# 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
include CarrierWave::MimeTypes
process :set_content_type
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process :resize_to_limit => [200, 200]
end
# store! nil's the cache_id after it finishes so we need to remember it for deletion
def remember_cache_id(new_file)
#cache_id_was = cache_id
end
def delete_tmp_dir(new_file)
# make sure we don't delete other things accidentally by checking the name pattern
if #cache_id_was.present? && #cache_id_was =~ /\A[\d]{8}\-[\d]{4}\-[\d]+\-[\d]{4}\z/
FileUtils.rm_rf(File.join(root, cache_dir, #cache_id_was))
end
end
end
the controller:
class NotesController < ApplicationController
before_filter :authenticate_user!, only: [:create, :destroy]
before_filter :correct_user, only: :destroy
def index
end
def create
#note = current_user.notes.build(params[:note])
#notes = Note.paginate(page: params[:page])
if #note.save
flash[:success] = "Note uploaded!"
redirect_to root_url
else
flash.now[:error] = "Note failed to upload."
render 'static_pages/home'
end
end
def destroy
#note.destroy
flash[:success] = "Note deleted."
redirect_to root_url
end
private
def correct_user
#note = current_user.notes.find_by_id(params[:id])
redirect_to root_url if #note.nil?
end
end
I don't really know if the problem lies in the gem or the controller. Even the controller is based on the microposts controller in Michael Hartl's book "The Ruby on Rails Tutorial" so I don't know where I have gone wrong.
That's an awfully early version of fog. The latest is 1.12.1. Try updating to the latest release using the following entry in your Gemfile
gem "fog", "~> 1.12.1"

Error upgrading to Rails 3.2 with Paperclip: write_inheritable_attribute

I'm upgrading an app from Rails 3.1 to 3.2 and I'm getting:
NoMethodError (undefined method `write_inheritable_attribute' for #<Class:0x0000010a190dd8>):
app/models/test_file.rb:37:in `<class:TestFile>'
app/models/test_file.rb:1:in `<top (required)>'
app/models/ability.rb:44:in `initialize'
The error applies to this model code:
has_attached_file :logo,
:styles => {:medium => "300x200>", :thumb => "150x60>" },
:storage => :s3,
:s3_credentials => "#{::Rails.root.to_s}/config/s3.yml",
:path => "/logos/:style/:id/:filename"
I have no instances of write_inheritable_attribute anywhere in my app's code. If the above is removed, the error repeats for the next instance of similar code in the app (i.e. everywhere Paperclip is used).
# Gemfile
gem 'rmagick'
gem "paperclip", :git => "git://github.com/thoughtbot/paperclip.git"
I think this issue may be with Paperclip's internal code somewhere. Do you know what's going on here?
:s3_credentials => "#{Rails.root}/config/s3.yml",

Error in Image crop using MiniMagick with carreirwave in Rails 3

I am trying to crop image using mini magick gem with carrierwave. I am facing following issue when i am creating user.
Errno::ENOENT in UsersController#create
No such file or directory - identify -ping /tmp/mini_magick20120919-5600-ai31ph.jpg
My Code :
model/user.rb
class User < ActiveRecord::Base
attr_accessor :crop_x, :crop_y, :crop_h, :crop_w
attr_accessible :username,:profile
after_update :reprocess_profile, :if => :cropping?
#field :username
mount_uploader :profile, ProfileUploader
def cropping?
!crop_x.blank? and !crop_y.blank? and !crop_h.blank? and !crop_w.blank?
end
def profile_geometry
#img = MiniMagick::Image.open(self.profile.large.path)
##geometry = {:width => img[:width], :height => img[:height] }
end
private
def reprocess_profile
#puts self.profile.large.path
#img = MiniMagick::Image.open(self.profile.large.path)
#crop_params = "#{crop_w}x#{crop_h}+#{crop_x}+#{crop_y}"
#img.crop(crop_params)
#img.write(self.profile.path)
#profile.recreate_versions!
end
end
uploader/profile_uploader.rb
# encoding: utf-8
class ProfileUploader < CarrierWave::Uploader::Base
# Include RMagick or ImageScience support:
#include CarrierWave::RMagick
# include CarrierWave::ImageScience
include CarrierWave::MiniMagick
# 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:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(jpg jpeg gif png)
end
version :large do
end
version :thumb do
process :resize_to_fill => [100, 100]
end
end
What would be the problem ? please suggest any solution.
Need to install imagemagick.
In ubuntu:
sudo apt-get install imagemagick

undefined method `within'

I followed the installation procedure of geokit-rails3, here is my conf :
Using rails (3.0.4)
Using activerecord (3.0.4)
Using geokit (1.5.0)
Using geokit-rails (1.1.4)
I get this error "undefined method `within' for #" when i try to query with the "within" method. (note that i am new to rails and maybe missing something obvious)
Here is my class definition :
class Snip < ActiveRecord::Base
belongs_to :user
acts_as_mappable :default_units => :kms,
:default_formula => :sphere,
:distance_field_name => :distance,
:lat_column_name => :latitude,
:lng_column_name => :longitude
end
In my controller i have :
#userLocation = GeoKit::LatLng.new(params[:lat],params[:lng])
#snips = Snip.within(params[:distance], :origin => #userLocation)
Here is what i added to my gemfile :
gem 'geokit', '>= 1.5.0'
gem 'geokit-rails', '1.1.4'
Do you have any idea why i get this error ?
Thanks in advance,
Vincent.
Your gemfile asks for geokit-rails, that's the Rails 2 version. You need to replace that with geokit-rails3