# Make link\_to\_remote call redirect current view, not read red

**URL:** <https://www.ruby-forum.com/t/make-link-to-remote-call-redirect-current-view-not-read-red/52008>\
**Category:** Rails\
**Created:** [January 10, 2006, 1:38am UTC](https://www.ruby-forum.com/t/make-link-to-remote-call-redirect-current-view-not-read-red/52008 "2006-01-10T01:38:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francois\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/53a042/32.png) [@Francois\_B](https://www.ruby-forum.com/u/Francois_B)\
**Post date:** [January 10, 2006, 1:38am UTC](https://www.ruby-forum.com/t/make-link-to-remote-call-redirect-current-view-not-read-red/52008/1 "2006-01-10T01:38:38Z")

</div>

Hello everyone !

I have a link\_to\_remote which creates a Party from a ContactRequest.  
The action on the server creates the party, marks the contact request  
as processed, and then returns a redirect. According to my knowledge  
of HTTP, that is the correct thing to do.

Unfortunately, Prototype is being too clever for me at this time…  
It follows the redirect, without notifying me. Anybody has any ideas  
on how I should be proceeding ? Workarounds or new solutions are  
accepted 🙂

Here’s my code, at the moment:  
\<%= link\_to\_remote(‘Create Party’,  
{ :url =\> {:action =\> ‘convert\_to\_party’, :id =\> request},  
:loading =\> visual\_effect(:fade, “create-#{request.id}”),  
:failure =\> %q{alert('Failure to update: ’ +  
request.responseText)},  
302 =\> %q(alert('Redirect To: ’ + request.inspect))},  
{ :title =\> ‘Convert to Party’, :id =\> “create-#{request.id}”  
})%\>

class DashboardController \< ApplicationController  
def convert\_to\_party  
@contact\_request = ContactRequest.find(params[:id])  
@party = @contact\_request.to\_party  
if @party.save then  
@contact\_request.mark\_processed  
redirect\_to :controller =\> ‘/admin/parties’, :action =\> :edit,  
:id =\> @party.id  
else  
render :inline =\> @party.errors.full\_messages.join("\n"),  
:status =\> ‘400 Bad Request’  
end  
end  
end

Thanks !

---

<div class="post-metadata">

**Author:** ![Francois\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/53a042/32.png) [@Francois\_B](https://www.ruby-forum.com/u/Francois_B)\
**Post date:** [January 10, 2006, 2:59am UTC](https://www.ruby-forum.com/t/make-link-to-remote-call-redirect-current-view-not-read-red/52008/2 "2006-01-10T02:59:57Z")

</div>

Hi Francois,

On 1/9/06, Francois B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a link\_to\_remote which creates a Party from a ContactRequest.  
> The action on the server creates the party, marks the contact request  
> as processed, and then returns a redirect. According to my knowledge  
> of HTTP, that is the correct thing to do.
> 
> Unfortunately, Prototype is being too clever for me at this time…  
> It follows the redirect, without notifying me.

Sorry – it’s not Prototype, but XMLHttpRequest, which doesn’t notify  
us of HTTP redirects. If you want to redirect the entire page, you’ll  
need to set the ‘window.location.href’ property in JavaScript at the  
appropriate time.

One way is to send the URL to redirect to as the response body, and  
set your :success callback to %( window.location.href =  
request.responseText ).

If you’re using edge Rails, an easier way is to use RJS’s  
page.redirect\_to method.

–  
sam

---

<div class="post-metadata">

**Author:** ![Francois\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/53a042/32.png) [@Francois\_B](https://www.ruby-forum.com/u/Francois_B)\
**Post date:** [January 10, 2006, 3:21am UTC](https://www.ruby-forum.com/t/make-link-to-remote-call-redirect-current-view-not-read-red/52008/3 "2006-01-10T03:21:06Z")

</div>

Hello Sam !

2006/1/9, Sam S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Sorry – it’s not Prototype, but XMLHttpRequest, which doesn’t notify  
> us of HTTP redirects. If you want to redirect the entire page, you’ll  
> need to set the ‘window.location.href’ property in JavaScript at the  
> appropriate time.

Ah, wasn’t aware of that. Thanks for the clarification.

> One way is to send the URL to redirect to as the response body, and  
> set your :success callback to %( window.location.href =  
> request.responseText ).

I implemented your solution. For posterity, here’s my action:  
def convert\_to\_party  
@contact\_request = ContactRequest.find(params[:id])  
@party = @contact\_request.to\_party  
if @party.save then  
@contact\_request.mark\_processed  
response.headers[‘Content-Type’] = ‘text/plain’  
render :inline =\> url\_for(:controller =\> ‘/admin/parties’,  
:action =\> :edit, :id =\> @party.id)  
else  
render :inline =\> @party.errors.full\_messages.join(“\n”),  
:status =\> ‘400 Bad Request’  
end  
end

And the view:  
\<%= link\_to\_remote(‘Create Party’,  
{ :url =\> {:action =\> ‘convert\_to\_party’, :id =\> request},  
:loading =\> visual\_effect(:fade, “create-#{request.id}”),  
:failure =\> %q{alert('Failure to update: ’ +  
request.responseText)},  
:success =\> %q(window.location.href = request.responseText)},  
{ :title =\> ‘Convert to Party’, :id =\> “create-#{request.id}”  
})%\>

Thanks a bunch !
