Topic: Trouble deleting record

Hello-

I am able to add and retrieve database entries, but I'm unable to delete a record.

Lots of blogs say that http://localhost:3000/users/1?_method=DELETE should do it. I enter that in the browser and it just displays entry '1'.

I also sent a POST request (I'm using Flex) with the url being http://localhost:3000/users/1 and { _method : "DELETE" } as parameters but the call fails saying "No action responded to 1. Actions: create, destroy, [....]"

Can someone please guide me? I've been searching all over.

Re: Trouble deleting record

Do you have a destroy action?

How are your routes setup?

What do the logs say?

Re: Trouble deleting record

Here's my destroy action for 'searches' (not 'users' like in my samples above):

  # DELETE /searches/1
  # DELETE /searches/1.xml
  def destroy
    @search = Search.find(params[:id])
    @search.destroy

    respond_to do |format|
      format.html { redirect_to(searches_url) }
      format.xml  { head :ok }
    end
  end

Ah, found this in the log:

Processing SearchesController#index to xml (for 127.0.0.1 at 2009-11-13 08:57:47) [GET]
  Search Load (13.6ms)   SELECT * FROM "searches" 
Completed in 79ms (View: 57, DB: 14) | 200 OK [http://localhost/searches.xml?]

Does that mean my POST is getting converted to GET?

Re: Trouble deleting record

Sorry, actually:

// here's the select working fine
Processing SearchesController#index to xml (for 127.0.0.1 at 2009-11-13 10:42:23) [GET]
  Search Load (81.6ms)   SELECT * FROM "searches" 
Completed in 173ms (View: 84, DB: 82) | 200 OK [http://localhost/searches.xml?]

// here's the delete failing
Processing SearchesController#3 (for 127.0.0.1 at 2009-11-13 10:42:29) [POST]
  Parameters: {"3"=>nil}

ActionController::UnknownAction (No action responded to 3. Actions: create, destroy, edit, index, new, show, and update):
 

Rendering rescues/layout (not_found)

Re: Trouble deleting record

Little bump here. Still haven't figured this out. Are people at a loss or is there more information I should provide? I feel like I've tried everything and it seems like it should be so simple.
Thanks.

Re: Trouble deleting record

I would suggest seeing what rails does with this:

<%= link_to "Delete me", my_record_to_delete, :method => :delete %>

Then inspect the html.

Re: Trouble deleting record

OK, thanks. So, when I do that, the html shows:
<a href="/searches?1=delete">Delete me</a>

So I tried that URL and it doesn't do anything other than list all my searches as if i just went to /searches/

I also tried a POST call with a parameter { "1" : "delete" } but that for some reason just inserts an empty search into the database.

Any further thoughts? This is pretty frustrating...

Re: Trouble deleting record

What was the erb that generated that html?
Show us the before and after, don't think there is anything in what I gave you that should set a param of 1 to delete ...

Re: Trouble deleting record

Ah, yes, I guess I did something wrong the first time.
I put this in the loop in me index.html.erb:
<td><%= link_to 'Delete me', search, :method => :delete %></td>

and I get:
<td><a href="/searches/76" onclick="var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit();return false;">Delete me</a></td>

Does that look more like what you're looking for?

Re: Trouble deleting record

Yes ... so now where did you receive the information (from what blogs) to pass in the _method=DELETE?
Maybe I can get more information from there to help you.

Re: Trouble deleting record

Thanks,

Here's one: http://www.informit.com/articles/articl … p;seqNum=3

It says: "As explained in the previous chapter, the Flash Player doesn’t support either the delete or put verbs, but Rails can instead take an additional request parameter named _method"

And it has an example but it doesn't work.

And here: http://stackoverflow.com/questions/6648 … in-the-url

Re: Trouble deleting record

reading here - http://rubyunderground.org/2009/11/swfu … -sessions/

So, Rails lets you do a POST, and tell it what verb you really meant through a parameter named _method.  Rails only respects _method, though, if it’s in the POST parameters.  So I can’t use the query-string to inject the psuedo PUT (or DELETE or …).

That is likely why rails turns it into a post then sets the _method.

Last edited by pullmonkey (2009-11-18 16:26:40)

Re: Trouble deleting record

Right. So, before I started this thread i tried a POST request with the url being http://localhost:3000/searches/2 and a parameter of { _method : "DELETE" } but the call fails saying "No action responded to 2. Actions: create, destroy, [....]"

I just tried it again and got the same thing. The log shows:

Processing SearchesController#2 (for 127.0.0.1 at 2009-11-18 13:55:14) [POST]
  Parameters: {"object Object"=>nil}

ActionController::UnknownAction (No action responded to 2. Actions: create, destroy, edit, index, new, show, and update):
 

Rendering rescues/layout (not_found)

Last edited by 99miles (2009-11-18 17:58:18)

Re: Trouble deleting record

So it thinks that 2 is the action, but it is really the id of your search? 

so put an action between searches and 2 ... /searches/<action>/2

Re: Trouble deleting record

omg, you rule. thanks so much!