質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

1回答

877閲覧

javascriptが読みこまれていますが、イベントが発火しない原因について

YUMIYUMO

総合スコア6

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2020/03/16 01:14

編集2020/03/16 02:19

イベントを発火させイメージ画像を投稿したいです。
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});

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

.fields_for :images do |image|以降のインデントが足りないために、.js-file_group{"data-index" => "#{image.index}"}がブロックに入っていないと認識されているのではないでしょうか。

(あと、ループのたびにjQuery本体を読み込んでいるのも意図がわからないです)

投稿2020/03/16 02:21

maisumakun

総合スコア145183

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

YUMIYUMO

2020/03/16 03:02 編集

ご回答ありがとうございます。 ループの度にjsを読み込めるようにしたのは、なぜか前にjsの非同期が発火しなかった時をこれをすることで解決できたからです。 今の状態としては、 https://gyazo.com/11ae6dacb625e27ebb3d6378865ec167 です。 console.logを確かめたところ$(document)については読み込まれていますが、それ以降読み込まれていません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問