Neville Burnell
2005-Apr-19 02:17 UTC
Redirect_to after POST [was Forwarding control to another controller and action]
Appropos of this topic, I had an action fired by a FORM which handled a
POST as follows:
def index
case @request.method
when :post
search = @params[''search'']
do_search(search)
end
End
This worked just fine except users couldn''t bookmark the resulting
view.
So I changed the controller to use a redirect as follows:
def index
case @request.method
when :post
redirect_to :action => ''list_matching'', :search =>
@params[''search'']
end
end
def list_matching
search = @params[''search'']
do_search(search)
End
This gives a bookmarkable action. Any comments welcome.
Nev
-----Original Message-----
From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On
Behalf Of Gavin Kistner
Sent: Tuesday, 19 April 2005 12:03 PM
To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails] Forwarding control to another controller and action
On Apr 18, 2005, at 7:54 PM, Gavin Sinclair wrote:>> def index
>> list
>> render_action( :list )
>> end
>
> How if this different from the following?
>
> def index
> redirect_to :action => ''list''
> end
>
> Why would you choose one over the other?
I''m just a rails noob, but I believe that redirect_to sends an HTTP
redirect response (HTTP 302, I think) like ASP''s Response.Redirect,
which causes the web browser to go fetch the new URL.
What I wrote (or rather, stole from a wiki page somewhere) send the
contents of the view as the response, without causing the browser to
change url (and with 1 less client/server roundtrip).
--
(-, /\ \/ / /\/
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
Ben Schumacher
2005-Apr-19 16:50 UTC
Re: Redirect_to after POST [was Forwarding control to another controller and action]
I''ve done a fair amount of POST-back implementations. According to the
HTTP specification, the preferred way to do this is to use status code
"303 See Other" instead of 302 which is what Rails will use by default
in these cases, so I''ve added these methods to my
ApplicationController:
def see_other(options = {})
see_other_url(url_for(options))
end
def see_other_path(path)
see_other_url(@request.protocol + @request.host_with_port + path)
end
def see_other_url(url)
render_text %Q[<html><body>You are being <a
href="#{url}">redirected</a>.</b
ody></html>]
@response.headers[''Status''] = ''303 See
Other''
@response.headers[''Location''] = url
@performed_redirect = true
end
Which gives me a compatible API, but is more protocol correct for
HTTP/1.1. While this status code isn''t supported by HTTP/1.0,
I''ve
never had a problem with the browsers I wish to support with my
application.
Cheers,
Ben
On 4/18/05, Neville Burnell
<Neville.Burnell-uEDVyssJ3mUpAS55Wn97og@public.gmane.org>
wrote:> Appropos of this topic, I had an action fired by a FORM which handled a
> POST as follows:
>
> def index
> case @request.method
> when :post
> search = @params[''search'']
> do_search(search)
> end
> End
>
> This worked just fine except users couldn''t bookmark the resulting
view.
> So I changed the controller to use a redirect as follows:
>
> def index
> case @request.method
> when :post
> redirect_to :action => ''list_matching'', :search
=> @params[''search'']
> end
> end
>
> def list_matching
> search = @params[''search'']
> do_search(search)
> End
>
> This gives a bookmarkable action. Any comments welcome.
>
> Nev
>
> -----Original Message-----
> From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On
Behalf Of Gavin Kistner
> Sent: Tuesday, 19 April 2005 12:03 PM
> To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> Subject: Re: [Rails] Forwarding control to another controller and action
>
> On Apr 18, 2005, at 7:54 PM, Gavin Sinclair wrote:
> >> def index
> >> list
> >> render_action( :list )
> >> end
> >
> > How if this different from the following?
> >
> > def index
> > redirect_to :action => ''list''
> > end
> >
> > Why would you choose one over the other?
>
> I''m just a rails noob, but I believe that redirect_to sends an
HTTP
> redirect response (HTTP 302, I think) like ASP''s
Response.Redirect,
> which causes the web browser to go fetch the new URL.
>
> What I wrote (or rather, stole from a wiki page somewhere) send the
> contents of the view as the response, without causing the browser to
> change url (and with 1 less client/server roundtrip).
>
> --
> (-, /\ \/ / /\/
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
David Morton
2005-Apr-19 20:57 UTC
Re: Redirect_to after POST [was Forwarding control to another controller and action]
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Neville Burnell wrote: | So I changed the controller to use a redirect as follows: | | def index | case @request.method | when :post | redirect_to :action => ''list_matching'', :search => @params[''search''] | end | end | | def list_matching | search = @params[''search''] | do_search(search) | End | | This gives a bookmarkable action. Any comments welcome. The usual way to do this in rails is to change the target of the form in ~ index.rhtml to list_matching, and then in list matching, redirect to a method "list" which will be bookmarkable. I like to keep my "post" actions seperate from my "get" actions. - -- David Morton Maia Mailguard server side anti-spam/anti-virus solution: http://www.maiamailguard.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.5 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFCZXDBSIxC85HZHLMRAl+HAJ9f/WCCVjxrsS94OyWbU7jeuBk1xgCfcow9 WztkqhJQ7h64Pul3hzZTaQ8=EPBj -----END PGP SIGNATURE-----