Format.html and redirect_to


#1

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


#2

On 28 Jan 2009, at 10:30, Ricky removed_email_address@domain.invalid 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


#3

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


#4

On 28 Jan 2009, at 23:42, Ricky wrote:

does result in actual content being sent back to the browser/AJAX
component?

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


#5

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