2013-02-19 44 views
1

我是ROR的新手,我試圖創建一個動作,我可以從數據庫中刪除一個文件。我寫了一個相同的代碼,但給url的錯誤。 查看了刪除操作: -在rails中創建一個動作的url

= link_to raw('<span>Delete</span>'), :method=> :delete, destroy_attachment_path(attachment.descendants.last), 
           :data => { :confirm => 'Are you sure? This will permanently delete this file!' }, 
           :remote => true, 
           :class => 'deleteShow deleteFile' 

控制器相同的: -

enter code here 

def destroy 

    @attachment = Attachment.find(params[:id]) 

    @attachment.destroy 

    respond_to do |format| 
    format.html { redirect_to attachments_url } 
    format.json { head :no_content } 
    end 
end 

當我試圖運行這段代碼錯誤出像無效的方法Destroy_attachment路徑。 任何人都可以幫助我找出問題嗎? 在此先感謝。

回答

0

您的link_to應該

= link_to raw('<span>Delete</span>'), attachment_path(attachment.descendants.last), 
    :method => :delete, 
    :data => { :confirm => 'Are you sure? This will permanently delete this file!' },    
    :remote => true, 
    :class => 'deleteShow deleteFile') 

,或者您可以使用嵌套的表格

= link_to attachment_path(attachment.descendants.last), 
    :method => :delete, 
    :data => { :confirm => 'Are you sure? This will permanently delete this file!' },    
    :remote => true, 
    :class => 'deleteShow deleteFile') do 
    %span Delete 

(注意的縮進,因爲你最有可能使用哈姆,我只是用這種方式來更好地閱讀代碼)

+0

再次感謝它效果更好。 。你能告訴我你是如何給這條道路的嗎,意思是給予行動路徑的邏輯是什麼..它會幫助我下次 – User16119012 2013-02-19 05:47:53

+0

這可能會幫助你更好地理解http://guides.rubyonrails.org/ routing.html#控制器的命名空間和路由 – jvnill 2013-02-19 05:51:34

1

你可以試試這個,它爲我工作..

link_to raw('<span>Delete</span>'), attachment.descendants.last, :method=> :delete,  :data => { :confirm => 'Are you sure? This will permanently delete this file!' }, 
          :remote => true, 
          :class => 'deleteShow deleteFile' 
+1

感謝這工作.... – User16119012 2013-02-19 05:30:54

+0

當我使用上述方法,我得到了這個確認消息。這個網頁正在被重定向到一個新的位置。你想重新發送你輸入的表單數據到新的位置嗎?當我檢查響應顯示 - 重新加載頁面獲取源代碼:http://0.0.0.0:3000/attachments/173。和ajaxified刪除不工作確認文件被刪除我必須重新加載頁面。有什麼建議麼。 – User16119012 2013-02-19 06:18:41

+0

有可能是與JavaScript的問題..請參閱此帖... http://stackoverflow.com/questions/4606860/rails-3-link-to-to-destroy-not-working – 2013-02-19 07:00:05

相關問題