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:
=== file: app/views/premises/_premise.html.erb
…
<%= link_to “delete”,
premise,
:method => :delete,
:confirm => “Positive?”,
:title => “Delete #{premise.address}” %>
… and the generated log entry:
Started GET “/premises/1” for 127.0.0.1 at 2011-01-26 13:33:19 -0800
Processing by PremisesController#show as HTML
Parameters: {“id”=>“1”}
Other details:
The partial is called from:
=== file: app/views/premises/index.html.erb
…
<%= render :partial => “premise”, :collection => @premises %>
Routing is restful:
=== file: config/routes.rb
Demo::Application.routes.draw do
…
resources :premises
…
Fearless F. wrote in post #977722:
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.
Best,
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
Marnen Laibow-Koser wrote in post #977726:
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:
params={“action”=>“show”, “controller”=>“premises”, “id”=>“2”}
So where did things go astray? (More constructively, where should I
look / how would you debug this?)
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.
Dai O. wrote in post #977736:
How do you set route to the delete action?
I’m just using the premise object as the second arg to link_to:
<%= link_to “delete”,
premise,
:method => :delete,
:confirm => “Positive?”,
:title => “Delete #{premise.address}” %>
The generated HTML is (for example):
delete
That looks basically right to me, so I’m still stumped.
Fearless F. wrote in post #977742:
Dai O. wrote in post #977736:
How do you set route to the delete action?
I’m just using the premise object as the second arg to link_to:
<%= link_to “delete”,
premise,
:method => :delete,
:confirm => “Positive?”,
:title => “Delete #{premise.address}” %>
…which can’t possibly work as you’d like. Rails is correctly
generating the path to the show action from the object you gave it.
The generated HTML is (for example):
delete
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.
Best,
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
Sent from my iPhone
Do you have something like this in routes.rb:
map.resources :premises
Marnen Laibow-Koser wrote in post #977756:
The generated HTML is (for example):
delete
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…)
Frederick C. wrote in post #977813:
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:
...
<%= javascript_include_tag :defaults %>
… and the resulting HTML looks like this:
...
Is that what you’d expect it to be?
On 27 Jan 2011, at 08:06, Fearless F. [email protected] wrote:
<%= javascript_include_tag :defaults %>
Is that what you’d expect it to be?
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:
Chris
On Jan 26, 10:50pm, Fearless F. [email protected] wrote:
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?
Fred
Chris M. wrote in post #977826:
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:
GitHub - rails/jquery-ujs: Ruby on Rails unobtrusive scripting adapter for jQuery
Chris
Give that man a cigar!
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’
[/Summary]
Thanks, all.
Frederick C. wrote in post #977813:
On Jan 26, 10:50pm, Fearless F. [email protected] wrote:
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!
Fred
Best,
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
Sent from my iPhone
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
Hope this helps!
Simon
Marnen Laibow-Koser wrote in post #977875:
I rarely use delete links…
That’s interesting!
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?
Fearless F. wrote in post #977888:
Marnen Laibow-Koser wrote in post #977875:
I rarely use delete links…
That’s interesting!
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.
Best,
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
Sent from my iPhone
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.
Ernst
What about this workaround: Define an additional route
match ‘localities/:id/delete’ => ‘localities#destroy’, :as =>
:delete_locality
Thanks
Ernst
Can you double-check you did everything listed here:
Also, have a look at your HTML source if all js files are included
(and in the correct order).
Simon
Hi I am a newbie,
So I followed the suggestions by Chris M. and Fearless F. but still
delete gets routed to show!
However this works:
curl “http://10.0.1.33:3000/localities/1” -X DELETE
Resoure locality was created with rails generate scaffold …
Thanks
Ernst