2012-09-14 98 views
1

我切換到Twitter的引導2.1.1。Twitter的引導模式軌道刪除按鈕不工作

我有一個模式與一個按鈕可以刪除,但它不能正常工作。在以前的引導版本中,它工作正常。 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

嘗試post_path(後)和檢查 – Amar

+0

感謝您的回覆@A三月 我試過這個 <%= link_to(t('delete'),post_path(post),:class =>'btn btn-primary')%> 但不起作用 – jferrercas

+1

檢查必要的javascripts(屬於bootstrap框架)在你生成的html中。 – inntran

回答

7

我發現http://rors.org/demos/custom-confirm-in-rails

以下解決方案可以完全不顯眼使用,而無需有一個自定義刪除您的視圖鏈接或模式的內容。所以,我認爲我有標準的Rails鏈接(只增加了一些類使用引導樣式):

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">&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) 

如果你想在你的模式後更具體的細節你可以隨時把它們作爲你的刪除鏈接數據 - 屬性(就像data-confirm用於顯示確認消息。)

+0

謝謝彼得,但它不爲我工作! – jferrercas

+0

對不起,我看了你的代碼,對我來說似乎沒問題。我沒有看到任何理由爲什麼你應該得到一個GET而不是一個POST請求。你有沒有和我的例子一樣的問題?仍然收到GET請求? –

+0

是,同樣的代碼,但我有bootstrap.min.js所以我貼你的JS代碼在一個新的文件bootstrap-confirmation.js.coffee – jferrercas