[英]Bootstrap Modal with <%= image_tag> in Rails
I apologize for the newbie question. 对于新手问题,我深表歉意。 I was trying to implement the Bootstrap Modal feature on my rails App.
我试图在我的rails应用程序上实现Bootstrap Modal功能。 Specifically, looking at applying to a
<%=image_tag>
so that when a user clicks on a specific image the modal is then activated. 具体来说,着眼于应用到
<%=image_tag>
以便当用户单击特定图像时会激活模态。 I looked at the following SO questions ( Twitter Bootstrap Modal Form Submit ) & ( Bootstrap open image in modal ) but after following the instructions, I still was not able to do it. 我查看了以下SO问题( Twitter Bootstrap模态表单Submit )和( Bootstrap模态中的打开图像 ),但是按照说明进行操作后,我仍然无法做到这一点。 I have provided all the relevant code below and thank you guys so much.
我已经在下面提供了所有相关代码,非常感谢你们。
Initially (Image in the modal is the same - the first one) across each each item 最初,每个项目中的图像(模态中的图像相同-第一个)
index.html.erb index.html.erb
<%= image_tag item.avatar.url(:medium), class: "block", id:"open-AddImgDialog", data: { target: "#myModal", toggle: "modal"} %>
<div class="modal fade" tabindex="-1" id="imagemodal" role="dialog">
<div class="modal-dialog" role="document" >
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<%= item.product %>
</div>
<br/>
<h4 class="modal-title">
<%= item.product %>
</h4>
<div class="modal-body">
<div class="picture">
<%= image_tag item.avatar.url(:medium) %>
</div>
<br/>
<br/>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
application.js application.js
$(document).on('click',"#open-AddImgDialog", function() {
$('#imagemodal').modal('show');
});
Attempt 3 (Add Javascript to change image to pick up current image for correct item > no image is appearing) 尝试3(添加Java脚本以更改图像以获取当前图像以获取正确的项目>没有图像出现)
index.html.erb index.html.erb
<%= image_tag item.avatar.url(:medium), class: "open-AddImgDialog", id:"test", data: { target: "#myModal", toggle: "modal"} %>
<div class="modal fade" tabindex="-1" id="imagemodal" role="dialog">
<div class="modal-dialog" role="document" >
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<%= item.product %>
</div>
<br/>
<h4 class="modal-title">
<%= item.product %>
</h4>
<div class="modal-body">
<div id="picture" style="border: 1px solid black; width: 200px; height: 200px;">
<canvas id="myCanvas" width="200" height="200"></canvas>
</div>
<br/>
<br/>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
application.js application.js
$(document).on('click',".open-AddImgDialog", function() {
html2canvas($("#picture"), {
onrendered: function (canvas) {
//theCanvas = canvas;
//document.body.appendChild(canvas);
//Canvas2Image.saveAsPNG(canvas);
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = document.getElementById("test");
var x = 0;
var y = 0;
var width = 200;
var height = 200;
ctx.drawImage(img, x, y, width, height);
}
});
});
用这个。
<%= image_tag item.avatar.url(:medium), class:"pop", :data => { :target => "#imagemodal } %>
Had to hire a professional developer to figure out exactly what was going on. 必须雇用专业的开发人员才能弄清楚到底发生了什么。 Here's the correct answer that worked and the explanation as to why - hopefully it helps someone along stuck in a similar bind.
这是有效的正确答案以及原因的解释-希望它可以帮助某个陷入困境的人。
The thing that is getting this stuck is that we have <% @items.each do |item| 被卡住的原因是我们有<%@ items.each | item | %> followed then by
<%= image_tag item.avatar.url(:medium), class: "open-AddImgDialog", id:"test", data: { target: "#myModal", toggle: "modal"} %>
and then the modal code listed above. %>,后跟
<%= image_tag item.avatar.url(:medium), class: "open-AddImgDialog", id:"test", data: { target: "#myModal", toggle: "modal"} %>
,然后是上面列出的模式代码。 This results in the same image (the first item image post) appearing over and over again when the modal is activated. 这样会导致在激活模式时一遍又一遍地出现同一张图片(第一张图片)。 When dealing with @items.each a solution to this problem would be to utilize a more flexible id that can dynamically change as we move from one item post to another(so that in the modal the same first image doesn't appear on all of them) .
当处理@items时,每个问题的解决方案是利用一个更灵活的id ,该id可以随着我们从一个项目发布移动到另一个项目而 动态更改(因此在模式中,相同的第一张图片不会出现在所有他们) 。
<%= image_tag item.avatar.url(:medium), class: "block", id: "image-#{dom_id(item)}", data: { target: "#myModal-#{dom_id(item)}", toggle: "modal"} %>
<div class="modal fade" tabindex="-1" id="myModal-<%= dom_id(item) %>" aria-labelledby="myModalLabel" role="dialog">
.....
#Everything else follows normal bootstrap conventions
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.