I read in the Agile Rails book that I should avoid GETs for
destructive actions. I notice that on 37signals’ Backpack web site,
deleting an item in a list is accomplished with a simple click on a
picture of a trash can. Is this a violation of the POST/GET rule
noted above, or is there some way to make a clickable link send a
POST?
Ben
If you?re using Rails, you can now do links like: link_to ?Destroy!?,
:action => ?destroy?, :post => true. That?ll create a link with an
onclick that uses POST for submit. If the user has js turned off, a GET
will still be send. On the server-side, you can then use verify
:destroy, :method => :post.
Yeah, it?s a bit cumbersome, but at least it?s there.
-Eric G.
ericgoodwin.com
Ben Nevile wrote:
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails
–
Eric G.
http://www.ericgoodwin.com
Thanks for your response, Eric. If I may be permitted a follow-up
question,
On the server-side, you can then use verify
:destroy, :method => :post.
Am I correct in assuming that this call should be in my
ApplicationController class? It’s the only controller in my app
that’s a direct subclass of ActionColler::Base.
if I add
verify :remove_from_chosen_labels, :method => :post
rails complains that I have the wrong number of arguments (it expects
one.) the call
verify :only => :remove_from_chosen_labels, :method => :post
sends me to a blank page, even though through the method you describe
above I have set post = TRUE (and this is verified in the URL of this
blank page.) if I say
verify :only => :remove_from_chosen_labels, :method => :post,
:redirect_to :index
I get a stack overflow.
Any hints for this newbie?
Thanks everyone,
Ben
|Hi Ben,
This should work.
class WhateverController < ActionController::Base
| verify :method => :post, :only => :remove_from_chosen_labels, :redirect_to => { :action=> “index” }
|
def |remove_from_chosen_labels
| # a GET request will never gain access to me
end
end
I’ve taken it from Loud Thinking and modified it a bit. If you check out
the post there is some more info on it.
http://www.loudthinking.com/arc/000529.html
If you are using route names and have and index url specified then you
can should be able to just have:
|:redirect_to => :index_url
I’ve included a few comments in your post below as well.
Cheers,
Eric G.
|
|
Ben Nevile wrote:
if I add
verify :remove_from_chosen_labels, :method => :post
You’re passing in a hash, you need to specify the key and the value. ie
key => value
This method can be rewritten as:
verify({:method => :post,:only =>:remove_from_chosen_labels })
rails complains that I have the wrong number of arguments (it expects
one.) the call
verify :only => :remove_from_chosen_labels, :method => :post
hmmm … not sure why this isn’t working
sends me to a blank page, even though through the method you describe
above I have set post = TRUE (and this is verified in the URL of this
blank page.) if I say
verify :only => :remove_from_chosen_labels, :method => :post,
:redirect_to :index
missing ‘=>’
http://lists.rubyonrails.org/mailman/listinfo/rails
–
Eric G.
http://www.ericgoodwin.com
To answer my own question,
the problem was that I actually was not generating POSTs. This is
because I had
<%= link_to “#{@chosen_labels[i].name}”, :action =>
‘remove_from_chosen_labels’, :id => @chosen_labels[i].id, :post =>
true %>
but I should have had
<%= link_to “#{@chosen_labels[i].name}”, {:action =>
‘remove_from_chosen_labels’, :id => @chosen_labels[i].id}, :post =>
true %>
Ben