I'm stumped. My link_to that is supposed to generate a DELETE is
generating a GET. And I don't understand why. Can anyone suggest what
I should be looking for to debug this? (The one thing I can think of is
that I'm including jquery.js and perhaps messed up the js?) Here's the
partial with the link_to:
I'm stumped. My link_to that is supposed to generate a DELETE is
generating a GET. And I don't understand why.
Browsers generally only support GET and POST. When Rails generates a
DELETE link, what it's actually doing is generating a GET link with
_method=delete. Rails parses the _method parameter and treats it
exactly as if it were a real DELETE request.
Browsers generally only support GET and POST. When Rails generates a
DELETE link, what it's actually doing is generating a GET link with
_method=delete. Rails parses the _method parameter and treats it
exactly as if it were a real DELETE request.
Hi Marnen. I understand your explanation. But my
PremisesController#show method is getting called, not
PremisesController#destroy method. At the time the show() method is
called, params looks like:
How do you set route to the delete action?
The other day I met same problem, I modified link_to's second
argument(after first "name" argument) to application path and then
worked fine at the time. Might fit into your case, maybe.
That looks basically right to me, so I'm still stumped.
How can that look right to you? There's nothing telling Rails that you
want DELETE or the destroy action. (The data-* attributes are
client-side, and Rails will never see them.)
What you need is something like destroy_premise_path(premise) for the
URL in link_to -- check rake routes for the exact name.
That looks basically right to me, so I'm still stumped.
How can that look right to you? There's nothing telling Rails that you
want DELETE or the destroy action. (The data-* attributes are
client-side, and Rails will never see them.)
Do you mean that the :method => :delete option has no effect on the
server? The documentation suggests something different.
What you need is something like destroy_premise_path(premise) for the
URL in link_to -- check rake routes for the exact name.
I thought the same thing, but rake routes gives me:
premises GET /premises(.:format) {:action=>"index",
:controller=>"premises"}
POST /premises(.:format) {:action=>"create",
:controller=>"premises"}
new_premise GET /premises/new(.:format) {:action=>"new",
:controller=>"premises"}
edit_premise GET /premises/:id/edit(.:format) {:action=>"edit",
:controller=>"premises"}
premise GET /premises/:id(.:format) {:action=>"show",
:controller=>"premises"}
PUT /premises/:id(.:format) {:action=>"update",
:controller=>"premises"}
DELETE /premises/:id(.:format) {:action=>"destroy",
:controller=>"premises"}
... which, if I read this correctly, means that the delete path is still
just called "premise_path" -- the same as GET and PUT, but using a
:destroy method instead.
So it still comes back to : how do I get link_to to generate a :destroy
action? I'm sure it's and obvious and stupid error on my part. (The
perplexing things is that I've done this lots of times before...)
Have you got the rails js loaded that detects those attributes and
actually does something with them?
@Fred: As I alluded to in the OP, I'm suspicious that I may have messed
up the default Rails JS when I included JQuery. My
app/views/layouts/application.html.erb file contains:
The default rails.js that ships with Rails 3 is designed to work with Prototype; you should replace it with one written to work on jQuery, if you haven't already:
The default rails.js that ships with Rails 3 is designed to work with
Prototype; you should replace it with one written to work on jQuery, if
you haven't already:
Evidently I'd installed the gem but missed the all important:
$ rails generate jquery:install
step. All better now.
[Summary for anyone who comes here via google]
If you're using jQuery, and if you notice that 'link_to obj, :method=>
:delete' is failing to trigger your controller's destroy() method, make
sure that:
(a) you have the correct routes set up -- do a rake routes to make sure
(b) you've installed the jQuery adaptor gem (and done a 'bundle
install')
(c) you've called 'rails generate jquery:install'
I'm using this successfully (in a Rails 2.3.5 app):
link_to("Delete Post", post_path(@post), :confirm => 'Are you
sure?', :method => :delete)
To my knowledge there is no such thing as a delete_XYZ_path.
Check your logs to see if DELETE is really received on the top level.
For debugging purposed try using curl: curl "localhost:3000/premises/
123" -X DELETE
That looks basically right to me, so I'm still stumped.
Have you got the rails js loaded that detects those attributes and
actually does something with them?
I rarely use delete links, so I didn't realize that it took JavaScript
to make one. This seems dysfunctional, and reinforces my belief that
having a link be anything other than GET represents a design problem.
Hmm.
My instinct is to suggest defining a GET destroy action. OTOH, that's
not idempotent. Aaugh!
My instinct is to suggest defining a GET destroy action. OTOH, that's
not idempotent. Aaugh!
Browsers only generate GETs and POSTs, so any DELETE action needs to be
simulated somehow. And as you point out, GETs (by contract) must be
idempotent. Which leave POST messages.
So I'm curious: how do you delete things? Write semi-custom forms that
POST something?
My instinct is to suggest defining a GET destroy action. OTOH, that's
not idempotent. Aaugh!
Browsers only generate GETs and POSTs, so any DELETE action needs to be
simulated somehow. And as you point out, GETs (by contract) must be
idempotent. Which leave POST messages.
So I'm curious: how do you delete things? Write semi-custom forms that
POST something?
Usually, I don't delete things from the Web interface. I generally hide
them (like acts_as_paranoid), use ActiveScaffold, or require deletion
from the console. That's not suitable for every application, obviously.
Hi,
I am new to rails and was very pleased to find this thread. After
reinstalling ruby with
rvm ( ruby-1.9.2-p0 [ x86_64 ])
installing rails 3.0.3
installing gem 'jquery-rails'
rails generate jquery:install
rails generate scaffold locality name:string type:string
parent_id:string
There is still the same problem: method delete gets routed to action
show.
Definig my own delete route may be a workaround. Having scaffolding
which does not work looks not right.