When calling destroy, record is not being removed from DB

ⅰ亾dé卋堺 提交于 2019-12-13 04:35:14

问题


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:


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' %>


来源:https://stackoverflow.com/questions/18467381/when-calling-destroy-record-is-not-being-removed-from-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!