When calling destroy, record is not being removed

2019-09-05 07:01发布

I've got a link_to meant to destroy a record:

<%= link_to 'Delete from DB', {:controller => 'foos', :action => 'destroy', :id => foo.id}, :class => "btn btn-primary btn-mini" %>

The destroy method in the controller:

  def destroy
    @foo = Foo.find(params[:id])
    @foo.destroy

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

Pressing the button doesn't delete the record, but if I open up rails console and call .destroy on an existing foo, it is removed from the DB.

Let me know if you see a mistake here. Thanks.

1条回答
2楼-- · 2019-09-05 07:51

It's probably because you send GET request while you need to sent DELETE request in order to call destroy action. So, your link should look:

<%= link_to 'Delete from DB', foo, :class => "btn btn-primary btn-mini", :method => 'delete' %>
查看更多
登录 后发表回答