我正在使用ruby 2.3.1
和rails 3.2.13
裁剪功能。我正在使用一個cropper.js JavaScript庫來裁剪圖像並將裁剪後的圖像添加到輸入字段以保存在服務器中。裁剪工作正常,但裁剪的圖像沒有得到保存。裁剪圖像不保存 - 紅寶石
brand.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裁剪功能:
_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'}
裁剪功能實現:
.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()'函數。 – jeffdill2