いつもお世話になっております!
RailsのCocoonを使ってネスト化された画像を追加していきたいと思っているのですが、下記のJSプレビューですと、追加してでてきた項目のプレビューidがユニークでないため、2枚目以降のプレビューを表示させることができません。
追加されるアイテムにidを振ることができれば良いのですが、何か良いアイデアなどはございますでしょうか。
開発環境
- Ruby 2.4.0
- Rails 5.2.2
- Cocoon 1.2.12
apps/views/users/new.html.erb
Ruby
1・・・ 2 <div class="formImage"> 3 <h3>画像</h3> 4 <%= f.fields_for :images do |m| %> 5 <div id="links"> 6 <%= link_to_add_association "画像を追加", f, :images, partial: 'image' %> 7 </div> 8 <% end %> 9 </div> 10・・・
apps/views/user/_image.html.erb
Ruby
1<div class="nested-fields"> 2 <div class="input-file updateIcon"> 3 <div class="imgPreview"> 4 <%= f.file_field :image, type: "file", id: "imageFile", accept: "image/*" %> 5 </div> 6 <%= link_to_remove_association "Remove image", f %> 7 </div> 8</div> 9 10<script data-turbolinks-eval="false"> 11 $("#imageFile").change(function() { 12 if (!this.files.length) { 13 return; 14 } 15 16 var file = $(this).prop("files")[0]; 17 var fr = new FileReader(); 18 $(".imgPreview").css("background-image", "none"); 19 fr.onload = function() { 20 $(".imgPreview").css("background-image", "url(" + fr.result + ")"); 21 }; 22 fr.readAsDataURL(file); 23 $(".imgPreview img").css("opacity", 0); 24 }); 25</script>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/23 20:45
2019/03/24 02:45 編集
2019/03/25 21:11