我正在使用 rail4 和 'bootstrap-sass':'3.3.6'。
我已按照 https://coderwall.com/p/ej0mhg/open-a-rails-form-with-twitter-bootstrap-modals 中的步骤操作并根据 Bootstrap modal in ruby on rails not working 重写了部分内容.
部分:
<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Modal header</h3>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
rails 控制台没有错误。
chrome inspect 的响应似乎没问题。
响应:
$("#myModal").find(".modal-content").html("<div aria-hidden=\'true\' aria-labelledby=\'myModalLabel\' class=\'modal fade\' id=\'myModal\' role=\'dialog\' tabindex=\'-1\'>\n<div class=\'modal-dialog\' role=\'document\'>\n<div class=\'modal-content\'>\n<div class=\'modal-header\'>\n<h3 id=\'myModalLabel\'>Modal header<\/h3>\n<\/div>\n<div class=\'modal-body\'>\n<p>One fine body…<\/p>\n<\/div>\n<div class=\'modal-footer\'>\n<button aria-hidden=\'true\' class=\'btn\' data-dismiss=\'modal\'>Close<\/button>\n<button class=\'btn btn-primary\'>Save changes<\/button>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n");
$("#myModal").modal('show');
但是模态框没有出现。
其他相关代码:
调用 Action 的 View :
%li= link_to 'Add release', new_release_path, {:remote => true, 'data-toggle' => "modal", 'data-target' => '#myModal'}
new_release.js.erb
$("#myModal").find(".modal-content").html("<%= j (render 'new_release') %>");
$("#myModal").modal('show');
路线:
get "new_release" => 'project#new_release', :as => :new_release
最佳答案
我想当您尝试通过 new_release.js.erb 中的 javascript 呈现该模态时
$("#myModal").find(".modal-content")
实际上它没有加载到 DOM 中,因为部分没有加载
因此,与其在内部添加整个模态,不如在您实际调用的模板文件中部分保留模型 div 标签,比如假设您的主模板是
index.html.erb
<%= link_to 'Add release', new_release_path, :remote => true %>
<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content"></div>
</div>
</div>
在 js.erb 中
new_release.js.erb
$("#myModal").find(".modal-content").html("<%= j (render 'new_release') %>");
$("#myModal").modal('show');
_new_release.html.erb
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Modal header</h3>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary">Save changes</button>
</div>
希望这有效,谢谢
关于jquery - ruby on rails 中的 Bootstrap 模态不工作(不显示),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47491685/