私は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クロップ機能前:
クロップ機能が実装さ.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()'関数をどこでも呼び出すことはありません。 – jeffdill2