【发布时间】:2017-03-08 05:50:32
【问题描述】:
我正在处理 ruby 2.3.1 和 rails 3.2.13 的裁剪功能。我正在使用cropper.js JavaScript 库来裁剪图像并将裁剪后的图像添加到输入字段以保存在服务器中。裁剪工作正常,但裁剪后的图像没有保存。
品牌.rb
class Brand < ActiveRecord::Base
belongs_to :company, :inverse_of => :brands
mount_uploader :image, AwsBrandImageUploader
end
crop_popup.haml
:javascript
jQuery(function($) {
$('#cropperImage').cropper({
aspectRatio: 16 / 9,
crop: function(dataNew) {
// Output the result data for cropping image.
}
});
});
function cropBrandImage() {
var croppedImage = $('#cropperImage').cropper('getCroppedCanvas').toDataURL('image/jpeg');
$('#image').attr('src', croppedImage);
$('[name="brand[image]"]').val(croppedImage); //add the cropped image to the input field using field name
}
_form.haml 裁剪前的特征:
.span6{:style=>"margin-left:60px"}
= f.label :image, label: '<span title="For best results upload an image with an aspect ration of 3 (wide) by 2 (high)."><i class="fa fa-info-circle fa-lg" aria-hidden="true"></i> Brand image</span>'
%div{style: 'width:201px;height:134px;border:3px solid #EEEEEE;background:#EEEEEE;', class: 'image_preview text-center'}
- if @brand.image?
= image_tag @brand.image_url(:mobile400), style: 'max-width:201px;max-height:134px;', id: 'image'
- else
%img(src='/assets/default_produce_image.jpg'){style: 'max-width:201px;max-height:134px;', id: 'image'}
= hidden_field(:image, :field, :value => @brand.image)
%div{:style => 'margin-top:15px;'}
= f.file_field :image, :class => 'filestyle', :onchange => 'imagePreview(this, "image", false)'
%span{:id => 'error_image'}
实现裁剪功能后的_form.haml:
.span6{:style=>"margin-left:60px"}
= f.label :image, label: '<span title="For best results upload an image with an aspect ration of 3 (wide) by 2 (high)."><i class="fa fa-info-circle fa-lg" aria-hidden="true"></i> Brand image</span>'
%div{style: 'width:201px;height:134px;border:3px solid #EEEEEE;background:#EEEEEE;', class: 'image_preview text-center'}
- if @brand.image?
= image_tag @brand.image_url(:mobile400), style: 'max-width:201px;max-height:134px;', id: 'image'
- else
%img(src='/assets/default_produce_image.jpg'){style: 'max-width:201px;max-height:134px;', id: 'image'}
= f.hidden_field(:image, :value => @brand.image)
%span{:id => 'error_image'}
我正在使用CarrierWave::MiniMagick 上传和保存图像文件。我正在做表单发布以保存数据。当我使用文件字段上传图像并单击保存时,将图像保存在服务器中。但是当我通过crop_popup裁剪相同的图像然后单击保存时,并没有保存图像。
我在弹出窗口中裁剪图像并使用字段名称将裁剪后的图像添加到输入字段。我还添加了作物实施前后的 .haml 文件更改。
请帮帮我。
谢谢。
【问题讨论】:
-
您的浏览器控制台是否出现任何错误?另外,我没有看到你在任何地方调用你的
cropBrandImage()函数。
标签: javascript ruby-on-rails ruby