Forum: Ruby on Rails Problems redirecting from form_remote_tag

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.
ginty (Guest)
on 2008-10-22 04:25
(Received via mailing list)
Hey,

A noob question no doubt but when I use form_remote_tag to make an
AJAX call to the server I can't make  it redirect to re-render the
entire page.

So for example the below code from /controller/index.html.erb invokes
the call...

<% form_remote_tag :url => {:action => :foo} do %>

  ...form stuff...

    <%= submit_tag "Submit" %>
<% end %>

And then a redirect from foo to index won't work...

def foo
      redirect_to :action => :index
end

Any thoughts?

Cheers!
ginty (Guest)
on 2008-10-22 04:47
(Received via mailing list)
Actually managed to fix it myself...

I found from the log that I was getting the following error:

ActionController::DoubleRenderError (Can only render or redirect once
per action):

And it seems that it was caused by the fact that I didn't realize that
the code flow continues in the action after the redirect.

So what I didn't show in the first message was that my action was
actually more like:

def foo
      if @failed
           redirect_to :action => :index      # I thought the code
flow would cease here if failed
      end
      blah blah
      redirect_to :action => :somewhere_else   # but actually it
continues and picks up this second redirect also
end
Bill W. (Guest)
on 2008-10-22 05:17
(Received via mailing list)
Hi ginty,

ginty wrote:
> I didn't realize that the code flow continues in the
> action after the redirect.
...
> def foo
>       if @failed
>            redirect_to :action => :index      # I thought the code
> flow would cease here if failed
>       end
>       blah blah
>       redirect_to :action => :somewhere_else   # but actually it
> continues and picks up this second redirect also
> end

To get it to cease where you thought it would, I believe you can add
'and
return' to your redirect.  Alternatively... yep, if you don't put the
'blah
blah..redirect_to" in an 'else' clause, it'll raise an error.

HTH,
Bil
This topic is locked and can not be replied to.