2012-07-04 72 views
5

我正在使用Rails 3,我有一個link_to與方法:刪除,但是當我點擊此鏈接它去顯示操作,而不是銷燬一個。刪除鏈接去顯示方法

我的問題是如何解決這個問題?

在此先感謝。

代碼:<%= link_to 'Delete', list_path(@list.id), :method => :delete, :confirm => 'Are you sure?' %>

+0

大概想看看類似這樣的問題:http://stackoverflow.com/questions/3774925/delete-link-sends-get-instead-of-delete-in-rails-3-view –

回答

6

你沒有你的application.js文件包括jqueryjquery_ujs庫,或者你甚至沒有包括在app/views/layouts/application.html.erbapplication.js文件。這通常是用這條線來完成:

<%= javascript_include_tag "application" %> 
0

你好你可以試試這個:

application.html.erb:

<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", "jquery.rails.js" %> 

和你鏈接的代碼應該是這樣的:

<%= link_to "<i class='icon-trash'></i> Delete".html_safe, item, :confirm => "Are you sure you want to delete item: " + item.name + "?" ,:method => :delete%> 

和你的控制器應該有這樣的:

def destroy 
@item = Item.find(params[:id]) 
if @item.destroy 
    redirect_to items_path, :notice => "Successfully deleted an item." 
else 
    redirect_to items_path, :notice => "Failed to delete an item." 
end 
end