Forum: Ruby on Rails how to make an ajax call to redirect

669c735cf55efa005140fa232b05c4f6?d=identicon&s=25 daociyiyou (Guest)
on 2008-10-16 13:29
(Received via mailing list)
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
        @user_cactions=@user.sponsored_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?
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2008-10-18 22:31
(Received via mailing list)
On Thu, Oct 16, 2008 at 1:24 AM, daociyiyou <cheyu22@yahoo.com.cn>
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:

<script type="text/javascript">document.location="/login"</script>

HTH,
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
5acba71393125496e93b9e434d8b63c9?d=identicon&s=25 Sjoerd Andringa (s-andringa)
on 2008-10-18 23:49
Hi daociyiyou ,

Try this:

def find_created_actions
    unless @if_login
       flash[:login]="..."
       redirect_to(:controller=>"login",:action=>"login")
    end
    if @if_login
       @user_cactions=@user.sponsored_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.
669c735cf55efa005140fa232b05c4f6?d=identicon&s=25 daociyiyou (Guest)
on 2008-10-20 09:36
(Received via mailing list)
On Oct 19, 5:49 am, Sjoerd Andringa <rails-mailing-l...@andreas-s.net>
wrote:
>        @user_cactio...@user.sponsored_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.
7ea637318b43f3a89c39cd0614af0a57?d=identicon&s=25 Vicer Ontero (vicer)
on 2010-09-24 04:21
Sjoerd Andringa wrote:
> Hi daociyiyou ,
>
> Try this:
>
> def find_created_actions
>     unless @if_login
>        flash[:login]="..."
>        redirect_to(:controller=>"login",:action=>"login")
>     end
>     if @if_login
>        @user_cactions=@user.sponsored_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:
http://stackoverflow.com/questions/1215177/cannot-...
5acba71393125496e93b9e434d8b63c9?d=identicon&s=25 Sjoerd Andringa (s-andringa)
on 2010-09-24 09:38
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:
> 
http://stackoverflow.com/questions/1215177/cannot-...

You're right. There's a confinient helper to generate the right
javascript
(http://apidock.com/rails/ActionView/Helpers/Protot...

def find_created_actions
    unless @if_login
        render :update do |page|
            flash[:login]="..."
            page.redirect_to(:controller=>"login",:action=>"login")
        end
    end
    [...]
end
7cd9ea64fa126c4e38573828d43df911?d=identicon&s=25 Glen (Guest)
on 2010-10-01 01:59
(Received via mailing list)
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
1b5c00ff827e0cbbe0f1610ca0660475?d=identicon&s=25 Jerome Fillioux (jayishappy)
on 2010-10-29 22:56
Glen's solution works perfect !
Thank you very much, your tip is a must have of every rails project,
IMHO :)
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.