Twitter Bootstrap模式rails删除按钮不起作用

jfe*_*cas 1 ruby-on-rails twitter-bootstrap

我转而使用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>
Run Code Online (Sandbox Code Playgroud)

模态

<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>
Run Code Online (Sandbox Code Playgroud)

但它不起作用.Rails接收GET并显示帖子,但它不会破坏它.

任何的想法?谢谢

Pet*_*tee 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'
Run Code Online (Sandbox Code Playgroud)

以及/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">&times;</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)
Run Code Online (Sandbox Code Playgroud)

如果您想在模式中添加更多帖子特定的详细信息,您可以将它们作为数据属性包含在删除链接中(就像data-confirm用于显示确认消息一样).