How to make an ajax call to redirect

I have an ajax call method like following:
##############################
def find_created_actions
unless @if_login
flash[:login]=“ÇëµÇ¼»ò×¢²á” redirect_to(:controller=>“login”,:action=>“login”)
end
if @if_login
@[email protected]_actions
render :partial=>“created_actions”,:object=>@user_cactions
end
end
###################################
I hope when some condition met,the call will let some partial replace
the html element’s
content,this works now.When the condition is not met,i hope redirect
to another
controller’s action,not replace something.but in fact,that action’s
template replace
the html element’s content,this is not the result i want.How do i
manage this?

On Thu, Oct 16, 2008 at 1:24 AM, daociyiyou [email protected]
wrote:

end
end
###################################
I hope when some condition met,the call will let some partial replace
the html element’s
content,this works now.When the condition is not met,i hope redirect
to another
controller’s action,not replace something.but in fact,that action’s
template replace
the html element’s content,this is not the result i want.How do i
manage this?

Instead of redirect_to render a partial with a line of JS like this:

HTH,

Hassan S. ------------------------ [email protected]

Hi daociyiyou ,

Try this:

def find_created_actions
unless @if_login
flash[:login]="…"
redirect_to(:controller=>“login”,:action=>“login”)
end
if @if_login
@[email protected]_actions
render :update do |page|
page.replace_html ‘element_id’,
:partial => “created_actions”, :object => @user_cactions
end
else
render :update do |page|
page.redirect_to(:controller=>“login”,:action=>“login”)
end
end
end

And make sure you’re not using the :update option on your remote_form.
‘page.redirect_to()’ will generate javascript similar to Hassan’s
aforementioned suggestion.

Sjoerd A. wrote:

Hi daociyiyou ,

Try this:

def find_created_actions
unless @if_login
flash[:login]=“…”
redirect_to(:controller=>“login”,:action=>“login”)
end
if @if_login
@[email protected]_actions
render :update do |page|
page.replace_html ‘element_id’,
:partial => “created_actions”, :object => @user_cactions
end
else
render :update do |page|
page.redirect_to(:controller=>“login”,:action=>“login”)
end
end
end

And make sure you’re not using the :update option on your remote_form.
‘page.redirect_to()’ will generate javascript similar to Hassan’s
aforementioned suggestion.

Hi, just a note that this does not work. Because the redirect gets
trapped inside the Ajax call. If you use firebug you can see what
happens.

So it seems the only way to redirect is to use a javascript redirect as
mentioned on:

On Oct 19, 5:49 am, Sjoerd A. [email protected]
wrote:

   @[email protected]_actions

And make sure you’re not using the :update option on your remote_form.
‘page.redirect_to()’ will generate javascript similar to Hassan’s
aforementioned suggestion.

Posted viahttp://www.ruby-forum.com/.

Thank you,it should work now.

Vicer Ontero wrote:

And make sure you’re not using the :update option on your remote_form.
‘page.redirect_to()’ will generate javascript similar to Hassan’s
aforementioned suggestion.

Hi, just a note that this does not work. Because the redirect gets
trapped inside the Ajax call. If you use firebug you can see what
happens.

So it seems the only way to redirect is to use a javascript redirect as
mentioned on:
asp.net - Cannot do response.redirect from page with Ajax controls - Stack Overflow

You’re right. There’s a confinient helper to generate the right
javascript
(redirect_to (ActionView::Helpers::PrototypeHelper::JavaScriptGenerator::GeneratorMethods) - APIdock):

def find_created_actions
unless @if_login
render :update do |page|
flash[:login]=“…”
page.redirect_to(:controller=>“login”,:action=>“login”)
end
end
[…]
end

Here is a cleaner way. Just put the following code on your
application_controller.rb.
Now, redirect_to should work with Ajax calls as well as regular
requests.

def redirect_to(options = {}, response_status = {})
if request.xhr?
render(:update) {|page| page.redirect_to(options)}
else
super(options, response_status)
end
end

Cheers,
Glen

Glen’s solution works perfect !
Thank you very much, your tip is a must have of every rails project,
IMHO :slight_smile: