format.html and redirect_to

741 views
Skip to first unread message

Ricky

unread,
Jan 28, 2009, 5:30:31 AM1/28/09
to Ruby on Rails: Talk
I was wondering what the purpose of wrapping a redirect_to in a
format.html block is. I get why you'd want to do this with a render,
but if you're redirecting the browser to another URL, what's the
point? As I understand it, when redirect_to is called, Rails sends a
302 back to the browser, so I'm having trouble understanding why
format plays any part in this. No biggie, just need some clarification
on what's actually happening.

Cheers,
Ricky

Frederick Cheung

unread,
Jan 28, 2009, 8:38:04 AM1/28/09
to rubyonra...@googlegroups.com


On 28 Jan 2009, at 10:30, Ricky <ri...@rickyrobinson.id.au> wrote:

>
> I was wondering what the purpose of wrapping a redirect_to in a
> format.html block is. I get why you'd want to do this with a render,
> but if you're redirecting the browser to another URL, what's the
> point? As I understand it, when redirect_to is called, Rails sends a
> 302 back to the browser,

It might not be a browser. Eg with a create action you would typically
redirect browsers to the show action, but the XML api probably
wouldn't do that.

Fred

Ricky

unread,
Jan 28, 2009, 6:42:31 PM1/28/09
to Ruby on Rails: Talk
> It might not be a browser. Eg with a create action you would typically  
> redirect browsers to the show action, but the XML api probably  
> wouldn't do that.

Thanks for responding Fred.

So are you saying that sometimes a redirect is not actually a redirect
in the 302 sense of the word? Rather that, sometimes, a redirect
*does* result in actual content being sent back to the browser/AJAX
component?

Rick

Frederick Cheung

unread,
Jan 28, 2009, 7:23:29 PM1/28/09
to rubyonra...@googlegroups.com
A redirect is always a redirect. What i'm saying is that sometimes you
don't want to redirect, in the aforementioned xml api example you'd
probably just respond with an empty response with the appropriate http
status code. An ajaxified interface is also a place where you might
not want to do a redirect (you'd just return some html to be inserted
into the DOM or produce some javascript to execute).

Fred

> Rick
> >

Ricky

unread,
Feb 23, 2009, 2:24:23 AM2/23/09
to Ruby on Rails: Talk
> A redirect is always a redirect. What i'm saying is that sometimes you  
> don't want to redirect, in the aforementioned xml api example you'd  
> probably just respond with an empty response with the appropriate http  
> status code. An ajaxified interface is also a place where you might  
> not want to do a redirect (you'd just return some html to be inserted  
> into the DOM or produce some javascript to execute).

What I wasn't understanding was that the format.xxx methods were
acting similarly to case statements. So the appropriate block of code
is executed depending on what the mime format is. With that tiny
tidbit of knowledge, it all becomes completely comprehensible.

Rick
Reply all
Reply to author
Forward
0 new messages