Rails 4.0.10:我一直在关注这个Jcrop教程:(http://railscasts.com/episodes/182-cropping-images),我已经提交了裁剪图像了。但是当我按下提交按钮时,我收到此错误:
NoMethodError in ThingsController#update
undefined method `sub' for ["-auto-orient", "-resize", "\"500x500\""]:Array
我看到了另一个带有相同错误的问题,这是通过将lib / paperclip_processor / cropper中的super.sub(/ -crop...
更改为“super.first.sub(/ -c...
来解决的,但这只是在我的情况下返回了相同的错误。有没有人否则Jcrop会遇到这个问题?
lib / paperclip_processors / cropper (我在错误页面上突出显示的行周围添加了星号)
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
视图/东西/ 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 %>
模型/ 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