イベントを発火させイメージ画像を投稿したいです。
scaffoldでcontroller等を作成させています。
bootstrapを使用しています。
一応、追記してあります。
エラー画面
ruby
1view/applications/new 2view/applications/_format 3= form_for @application do |f| 4 - if @application.errors.any? 5 #error_explanation 6 %h2= "#{pluralize(@application.errors.count, "error")} prohibited this application from being saved:" 7 %ul 8 - @application.errors.full_messages.each do |message| 9 %li= message 10 11 .field 12 = f.label :出願人 13 = f.text_field :applicant 14 .field 15 = f.label :出願日 16 = f.text_field :application_day 17 .field 18 = f.label :出願番号 19 = f.text_field :application_number 20 .field 21 = f.label :料金 22 = f.text_field :application_fee 23 .field 24 = f.label :指定商品・役務 25 = f.text_field :designated_product 26 .fields_for :images do |image| 27 %script{:src => "https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"} 28 .js-file_group{"data-index" => "#{image.index}"} 29 = image.file_field :src, class: 'js-file' 30 %br/ 31 %span.js-remove 削除 32 - if @application.persisted? 33 = image.check_box :_destroy, data:{ index: image.index }, class: 'hidden-destroy' 34 - if @application.persisted? 35 .js-file_group{"data-index" => "#{@application.images.count}"} 36 = file_field_tag :src, name: "application[images_attributes][#{@application.images.count}][src]", class: 'js-file' 37 .js-remove 削除 38 39 .actions 40 = f.submit 'Save' 41 42
ruby
1applications/js 2 3$(document).on('turbolinks:load', () => { 4 // 画像用のinputを生成する関数 5 const buildFileField = (index) => { 6 const html = `<div data-index="${index}" class="js-file_group"> 7 <input class="js-file" type="file" 8 name="applications[images_attributes][${index}][src]" 9 id="applications_images_attributes_${index}_src"><br> 10 <div class="js-remove">削除</div> 11 </div>`; 12 return html; 13 } 14 15 // プレビュー用のimgタグを生成する関数 16 const buildImg = (index, url) => { 17 const html = `<img data-index="${index}" src="${url}" width="100px" height="100px">`; 18 return html; 19 } 20 21 // file_fieldのnameに動的なindexをつける為の配列 22 let fileIndex = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 23 // 既に使われているindexを除外 24 lastIndex = $('.js-file_group:last').data('index'); 25 fileIndex.splice(0, lastIndex); 26 27 $('.hidden-destroy').hide(); 28 29 $('#image-box').on('change', '.js-file', function (e) { 30 const targetIndex = $(this).parent().data('index'); 31 // ファイルのブラウザ上でのURLを取得する 32 const file = e.target.files[0]; 33 const blobUrl = window.URL.createObjectURL(file); 34 35 // 該当indexを持つimgがあれば取得して変数imgに入れる(画像変更の処理) 36 if (img = $(`img[data-index="${targetIndex}"]`)[0]) { 37 img.setAttribute('src', blobUrl); 38 } else { // 新規画像追加の処理 39 $('#previews').append(buildImg(targetIndex, blobUrl)); 40 // fileIndexの先頭の数字を使ってinputを作る 41 $('#image-box').append(buildFileField(fileIndex[0])); 42 fileIndex.shift(); 43 // 末尾の数に1足した数を追加する 44 fileIndex.push(fileIndex[fileIndex.length - 1] + 1); 45 } 46 }); 47 48 $('#image-box').on('click', '.js-remove', function () { 49 const targetIndex = $(this).parent().data('index'); 50 // 該当indexを振られているチェックボックスを取得する 51 const hiddenCheck = $(`input[data-index="${targetIndex}"].hidden-destroy`); 52 // もしチェックボックスが存在すればチェックを入れる 53 if (hiddenCheck) hiddenCheck.prop('checked', true); 54 55 $(this).parent().remove(); 56 $(`img[data-index="${targetIndex}"]`).remove(); 57 58 // 画像入力欄が0個にならないようにしておく 59 if ($('.js-file').length == 0) $('#image-box').append(buildFileField(fileIndex[0])); 60 }); 61});

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/03/16 03:02 編集