Francois Beausoleil
2006-Jan-10 00:34 UTC
[Rails] Make link_to_remote call redirect current view, not read redirected content
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 !
--
Fran?ois Beausoleil
http://blog.teksol.info/
Sam Stephenson
2006-Jan-10 00:58 UTC
[Rails] Make link_to_remote call redirect current view, not read redirected content
Hi Francois, On 1/9/06, Francois Beausoleil <francois.beausoleil@gmail.com> 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
Francois Beausoleil
2006-Jan-10 02:20 UTC
[Rails] Make link_to_remote call redirect current view, not read redirected content
Hello Sam ! 2006/1/9, Sam Stephenson <sstephenson@gmail.com>:> 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 ! -- Fran?ois Beausoleil http://blog.teksol.info/