In Onlamp there is a wonderful introduction to Ajax under Rails in "Ajax on Rails," ( http://www.onlamp.com/pub/a/onlamp/2005/06/09/rails_ajax.html ). I have taken the Author, Curt Hibbs'' code from the example, and in looking at other examples, a common seeming problem (to me) keeps recurring. If I want to render to a < div > element, when I make a subsequent render to it, it then shows both this rendering as well as the previous -- when in fact, I only want the latest rendering. The code for an rhtml as well as a method coinciding with that rhtml bears this out. How can I amend such code so as to only print the last link_to_remote result? Also, would there be a way to render to, say, a textbox in this example? Thank you. Ive looked at other Ajax on Rails examples and they all seem to have this same ''problem,'' which I''d like to get around. <html> <head> <title>Ajax Demo</title> <%= javascript_include_tag :defaults %> </head> <body> <h1>What time is it?</h1> <div id="time_div"> I don''t have the time, but <%= link_to_remote( "click here", :update => "time_div", :url => { :action => :say_when }, :position => "bottom" ) %> and I will look it up. </div> </body> </html> def say_when render_text "<p>The time is <b>" + DateTime.now.to_s + "</b></p>" end --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
cremes.devlist-ee4meeAH724@public.gmane.org
2006-Nov-12 14:06 UTC
Re: Making Ajax rerender in div
On Nov 12, 2006, at 5:27 AM, Ike wrote:> If I want to render to a < div > element, when I make a subsequent > render to it, it then shows both this rendering as well as the > previous > -- when in fact, I only want the latest rendering. The code for an > rhtml as well as a method coinciding with that rhtml bears this out. > How can I amend such code so as to only print the last link_to_remote > result? Also, would there be a way to render to, say, a textbox in > this > example? > > Thank you. Ive looked at other Ajax on Rails examples and they all > seem > to have this same ''problem,'' which I''d like to get around. > > <html> > <head> > <title>Ajax Demo</title> > <%= javascript_include_tag :defaults %> > </head> > <body> > <h1>What time is it?</h1> > <div id="time_div"> > I don''t have the time, but > <%= link_to_remote( "click here", > :update => "time_div", > :url => { :action => :say_when }, > :position => "bottom" ) %> > and I will look it up. > </div> > </body> > </html> > > > def say_when > render_text "<p>The time is <b>" + DateTime.now.to_s + "</b></ > p>" > endTry changing the code to render a partial. Also, don''t use #render, use #replace_html. Try this: def say_when replace_html "time_div", :partial => "time", end The problem here will be the entire div will get replaced by whatever HTML is in the partial. The "I don''t have the time..." text will all get blown away so the user will only get to click on this once. cr --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> <%= link_to_remote( "click here", > :update => "time_div", > :url => { :action => :say_when }, > :position => "bottom" ) %>When you say :position => "bottom", that means that you want to stick the result of the render at the bottom of the div. remove the :position and you should be fine. The action that is called via link_to_remote is just the same as any old action, so you can render whatever rhtml you want, so if you want a textfield it''s no different to usual. You can also use rjs if you need to update more than one thing at once. Fred -- 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 -~----------~----~----~----~------~----~------~--~---