Forum: Ruby on Rails POST vs GET

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
91ac2635d306867b3fb429d28c53ef3b?d=identicon&s=25 Ben Nevile (Guest)
on 2006-05-13 00:15
(Received via mailing list)
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
Ac1da5d66d51a1c3b843221032247b30?d=identicon&s=25 Eric Goodwin (Guest)
on 2006-05-13 00:24
(Received via mailing list)
Hi,
Here is a quote from DHH. You can find it here:
http://www.37signals.com/svn/archives2/the_google_...
----

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 Goodwin
ericgoodwin.com


Ben Nevile wrote:
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails


--
Eric Goodwin
http://www.ericgoodwin.com
91ac2635d306867b3fb429d28c53ef3b?d=identicon&s=25 Ben Nevile (Guest)
on 2006-05-13 02:15
(Received via mailing list)
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
91ac2635d306867b3fb429d28c53ef3b?d=identicon&s=25 Ben Nevile (Guest)
on 2006-05-13 02:21
(Received via mailing list)
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
Ac1da5d66d51a1c3b843221032247b30?d=identicon&s=25 Eric Goodwin (Guest)
on 2006-05-13 02:30
(Received via mailing list)
|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 Goodwin
|
|

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 Goodwin
http://www.ericgoodwin.com
This topic is locked and can not be replied to.