Forum: Ruby on Rails Make link_to_remote call redirect current view, not read red

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Francois B. (Guest)
on 2006-01-10 02:38
(Received via mailing list)
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 !
Sam S. (Guest)
on 2006-01-10 03:59
(Received via mailing list)
Hi Francois,

On 1/9/06, Francois B. <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
Francois B. (Guest)
on 2006-01-10 04:21
(Received via mailing list)
Hello Sam !

2006/1/9, Sam S. <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 !
This topic is locked and can not be replied to.