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=-WvErx+DwRRWoKACRLwQjhg@public.gmane.org_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?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Thu, Oct 16, 2008 at 1:24 AM, daociyiyou <cheyu22@yahoo.com.cn> wrote:> > 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?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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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-do-response-redirect-from-page-with-ajax-controls -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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-do-response-redirect-from-page-with-ajax-controlsYou''re right. There''s a confinient helper to generate the right javascript (http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods/redirect_to): def find_created_actions unless @if_login render :update do |page| flash[:login]="..." page.redirect_to(:controller=>"login",:action=>"login") end end [...] end -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Glen''s solution works perfect ! Thank you very much, your tip is a must have of every rails project, IMHO :) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.