我正在转向Twitter Bootstrap 2.1.1。
我有一个带有要删除的按钮的模态,但它不起作用。在之前的bootstrap版本中它运行正常。 Rails版本是3.1
这是代码
<a title="<%= t('delete') %>" id="delete" class="label" href="#myModal-<%= post.id %>" data-toggle="modal"><%= t('delete') %></a>
模态
<div class="modal hide" id="myModal-<%= post.id %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel"><%= t('delete_this_question') %></h3>
</div>
<div class="modal-body">
<p><%= raw post.text %></p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true"><%= t('cancel') %></button>
<%= link_to(t('delete'), { :controller => 'posts', :action => 'destroy', :id => post.id } ,:method => :delete, :class => 'btn btn-primary') %>
</div>
</div>
但它不起作用。 Rails接收GET并显示帖子,但它不会破坏它。
有什么想法吗?感谢
答案 0 :(得分:7)
我在http://rors.org/demos/custom-confirm-in-rails
上找到了以下解决方案您可以完全不引人注意地使用此功能,而无需在视图中使用自定义删除链接或模态内容。所以在我看来,我有标准的Rails链接(只添加一些类来使用Bootstrap样式):
link_to 'delete', post, method: :delete, confirm: 'Are you sure?', class: 'btn btn-mini btn-danger'
以下是/app/assets/javascripts/bootstrap-confirmation.js.coffee
$.rails.allowAction = (link) ->
return true unless link.attr('data-confirm')
$.rails.showConfirmDialog(link) # look bellow for implementations
false # always stops the action since code runs asynchronously
$.rails.confirmed = (link) ->
link.removeAttr('data-confirm')
link.trigger('click.rails')
$.rails.showConfirmDialog = (link) ->
message = link.attr 'data-confirm'
html = """
<div class="modal" id="confirmationDialog">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Request confirmation</h3>
</div>
<div class="modal-body">
<p>#{message}</p>
</div>
<div class="modal-footer">
<a data-dismiss="modal" class="btn">Cancel</a>
<a data-dismiss="modal" class="btn btn-danger confirm">Confirm</a>
</div>
</div>
"""
$(html).modal()
$('#confirmationDialog .confirm').on 'click', -> $.rails.confirmed(link)
如果您想在模式中添加更多帖子特定的详细信息,您可以随时将它们作为数据属性包含在删除链接中(就像data-confirm
用于显示确认消息一样。)