santu47
santu47

Reputation: 652

delete is not working, instead it is inserting a new empty record

I've tried a lot to delete a record from my DB, but I have failed. Instead of deleting the selected value it is inserting a new empty record into my db. Please help me out.

My Controller is:

class CatvaluesController < ApplicationController
    ...
    def destroy
        @catvalue = Catvalue.find(params[:id])
        @catvalue.destroy
        redirect_to catvalues_path
    end
    ....
end

and my form is:

<%= form_for(@catvalue) do |f| %>
<%= f.collection_select :id, @catvalues, :id, :v_name, {}, class: 'drop-down'%>
<%= f.submit 'Destroy', :confirm => 'Are you sure you want to destroy your account?' %>
<% end %>

Upvotes: 0

Views: 65

Answers (2)

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

form_for by default takes post method

<%= form_for @catvalue, :method => :delete do |f| %>

Okay, so I'm now adding url to the form helper, try this one!

<%= form_for @catvalue, :url => "/catvalues/#{@catvalue.id}",:method => :delete do |f| %>

Upvotes: 1

Intrepidd
Intrepidd

Reputation: 20938

You are submitting a POST request, it will call the create action on your controller hence your empty model.

You have to use the delete http method to call the destroy action on your controller :

<%= form_for(@catvalue, :method => :delete) do |f| %>

Upvotes: 0

Related Questions