Rails 4.0.10: I've been following this Jcrop tutorial: (http://railscasts.com/episodes/182-cropping-images), and I've gotten as far as submitting the cropped image. But when I press the submit button, I get this error:
NoMethodError in ThingsController#update
undefined method `sub' for ["-auto-orient", "-resize", "\"500x500\""]:Array
I saw another question with the same error, which was fixed by changing super.sub(/ -crop...
in lib/paperclip_processor/cropper to "super.first.sub(/ -c...
, but this just returned the same exact error in my case. Has anyone else run into this issue with Jcrop?
lib/paperclip_processors/cropper (I put asterisks around the line highlighted on the error page)
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?
" -crop '#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}' "
end
end
end
end
views/things/crop.html.erb
<% content_for(:head) do %>
<%= stylesheet_link_tag "jquery.Jcrop" %>
<%= javascript_include_tag "jquery.Jcrop.min" %>
<script type="text/javascript" charset="utf-8">
ready = $(function() {
$("#cropbox").Jcrop({
onChange: update_crop,
onSelect: update_crop,
setSelect: [0, 0, 500, 500],
aspectRatio: 1
});
});
function update_crop(coords) {
$("#crop_x").val(coords.x);
$("#crop_y").val(coords.y);
$("#crop_w").val(coords.w);
$("#crop_h").val(coords.h);
}
$(document).ready(ready);
$(document).on('page:load', ready);
</script>
<% end %>
<%= image_tag @thing.avatar.url(:large), :id => "cropbox" %>
<%= form_for @thing do |f| %>
<% for attribute in [:crop_x, :crop_y, :crop_w, :crop_h] %>
<%= f.text_field attribute, :id => attribute %>
<% end %>
<p><%= f.submit "Crop" %></p>
<% end %>
models/thing.rb
class Thing < ActiveRecord::Base
# ...
has_attached_file :avatar, :styles => { :large => "500x500", :medium => "300x300>", :thumb => "50x50!" }, :processors => [:cropper]
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
after_update :reprocess_avatar, :if => :cropping?
def cropping?
!crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end
private
def reprocess_avatar
avatar.reprocess!
end
end