如果您想在隐藏状态的页面上预加载模式,下面是代码
<%= link_to "Open modal", "#my-modal", :class => "btn", "data-toggle" => "modal" %>
<div class="modal hide fade" id="my-modal" title="My modal">
<div class="modal-header">
<button aria-hidden="true" class="close" data-dismiss="modal" type="button">×</button>
<h3 id="myModalLabel">Modal header</h3>
</div>
<div class="modal-body">
Modal Body
</div>
<div class="modal-footer">
<button aria-hidden="true" class="btn" data-dismiss="modal">Close</button>
</div>
</div>
如果你想通过 ajax 加载模态,那么你可以做这样的事情
<%= link_to "Open modal", new_post_path, :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "my-modal" %>
<div class="modal hide fade" id="my-modal" title="My modal">
<div class="modal-header">
<button aria-hidden="true" class="close" data-dismiss="modal" type="button">×</button>
<h3 id="myModalLabel">New Post</h3>
</div>
<div class="modal-body a-unique-class">
New Post Body
</div>
<div class="modal-footer">
<button aria-hidden="true" class="btn" data-dismiss="modal">Close</button>
</div>
</div>
在posts/new.js.erb
你会包括
$(".a-unique-class").html('<%= j render "posts/_form" %>')
确保每个模态体都有一个唯一的id 或类。
假设您要使用模态表单创建新帖子,控制器代码和_form.html.erb
到位