2011-06-02 215 views
1

我正试图做一个简单的删除记录在数据库中。当我尝试删除一条记录时,它创建一个新的记录

<b> <%= button_to "Cancel your invitation", { :controller=>"invites", :method => 'destroy', :id => invite.id }, 
:confirm => "Are you sure you want to delete this invite?" %></b> 

我指定控制器,因为我正在从用户视图执行此操作。

当我点击按钮时,它会为新记录创建一个空白条目。那么为什么当我指定一个销毁方法时会使用create方法呢?

一些更成: 我的配置/路线很简单,只要可以是:

资源:邀请

我的控制器是很基本的太:

def create 
    @invite = Invite.new(params[:invite]) 
    if @invite.save 
     redirect_to current_user 
    else 
     redirect_to root_path 
    end 
    end 



    def destroy 
    Invite.find(params[:id]).destroy 
    redirect_to current_user 
    end 
+0

你的控制器是什么样的? – 2011-06-02 17:21:11

回答

1

您误用了这些方法。它应该是:

<b> 
    <%= button_to "Cancel your invitation", invite, :confirm => "Are you sure you want to delete this invite?", :method => :delete %> 
</b> 

有2分。

1)对于resources :invites,销毁方法需要delete。如果不通过:method => :deletebutton_to默认会使用:method => :post,这是创建方法。这就是为什么你创造了一个新纪录而不是销毁它。

2)当你正在使用的资源,你有更好的选择的路线,而不是{:controller => "invites", :action => "destroy", :id => invite.id}(注意,这是:action

你可以使用invite_path(invite)或只是invite就像我上面的例子。随着:method => :delete这给你的销毁方法。 (so with:post for create,:put for update)

+0

谢谢,我认为这可行不要太确定,因为我试图删除时出现错误),所以我现在将这个标记为正确的。 – 2011-06-02 17:35:00

+0

我的控制器是否应该销毁操作? – 2011-06-02 17:47:42

+1

对控制器和路由部分没有改变(使用'resources:invites'和标准的销毁行为是可以的) – PeterWong 2011-06-03 05:08:38

2

我想想,你需要使用删除方法而不是销毁。

+0

+1,你应该在你的邀请控制器中有一个名为destroy的动作。 – Spyros 2011-06-02 17:24:13

+0

@SpyrosP:是的,它应该在那里:) – 2011-06-02 17:25:54

+0

同样的问题:方法=>'删除' – 2011-06-02 17:28:14