My controller ends like this: render :update do |page| page.replace_html ''someDiv'', :partial => ''somePartial'' end I need to edit this to verify that someDiv exists before I go replacing_htmling it. I''m at a loss to figure out if this can be done. Any ideas? Thanks! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 8/31/07, Josh <josh.m.sharpe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > My controller ends like this: > > render :update do |page| > page.replace_html ''someDiv'', :partial => ''somePartial'' > end > > I need to edit this to verify that someDiv exists before I go > replacing_htmling it. I''m at a loss to figure out if this can be > done. Any ideas?are you not in control of the content of your pages? As far as I know, the only way to do something like this would be to read the entire page and search to make sure the div exists before continuing. Unfortunately you can''t just execute the rjs call and expect it to throw an exception on error (the exception is thrown outside the context of rails and handled directly by the prototype library) You can use the following library to allow you to parse your html files: http://code.whytheluckystiff.net/hpricot/ but of course it''ll be faster when operating on local files than through using open-uri. But this is an expensive operation which you really want to avoid if possible. Adam --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
rein.henrichs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Sep-01 14:41 UTC
Re: verifying existence of elements
Parse the entire page with hpricot? That''s rather silly. You can use basic javascript or the prototype library to check for the existance of an element with an id, and you can use use the concatenation operator to add plain javascript to the output: page << ''if (${''someDiv'')) {'' page.replace_html ''someDiv'', :partial => ''somePartial'' page << ''}'' It''s not the most elegant solution because you''re not writing pure ruby any more, but it''s quick and simple if you know a little javascript. Rein On Aug 31, 11:31 am, "Adam Cohen" <bionicboo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 8/31/07, Josh <josh.m.sha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > My controller ends like this: > > > render :update do |page| > > page.replace_html ''someDiv'', :partial => ''somePartial'' > > end > > > I need to edit this to verify that someDiv exists before I go > > replacing_htmling it. I''m at a loss to figure out if this can be > > done. Any ideas? > > are you not in control of the content of your pages? As far as I > know, the only way to do something like this would be to read the > entire page and search to make sure the div exists before continuing. > Unfortunately you can''t just execute the rjs call and expect it to > throw an exception on error (the exception is thrown outside the > context of rails and handled directly by the prototype library) > > You can use the following library to allow you to parse your html files: > > http://code.whytheluckystiff.net/hpricot/ > > but of course it''ll be faster when operating on local files than > through using open-uri. But this is an expensive operation which you > really want to avoid if possible. > > Adam--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
rein.henrichs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Sep-01 17:20 UTC
Re: verifying existence of elements
Whoops, typo: page << ''if (${''someDiv'')) {'' should be: page << ''if ($(''someDiv'')) {'' On Sep 1, 9:41 am, rein.henri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Parse the entire page with hpricot? That''s rather silly. You can use > basic javascript or the prototype library to check for the existance > of an element with an id, and you can use use the concatenation > operator to add plain javascript to the output: > > page << ''if (${''someDiv'')) {'' > page.replace_html ''someDiv'', :partial => ''somePartial'' > page << ''}'' > > It''s not the most elegant solution because you''re not writing pure > ruby any more, but it''s quick and simple if you know a little > javascript. > > Rein > On Aug 31, 11:31 am, "Adam Cohen" <bionicboo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On 8/31/07, Josh <josh.m.sha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > My controller ends like this: > > > > render :update do |page| > > > page.replace_html ''someDiv'', :partial => ''somePartial'' > > > end > > > > I need to edit this to verify that someDiv exists before I go > > > replacing_htmling it. I''m at a loss to figure out if this can be > > > done. Any ideas? > > > are you not in control of the content of your pages? As far as I > > know, the only way to do something like this would be to read the > > entire page and search to make sure the div exists before continuing. > > Unfortunately you can''t just execute the rjs call and expect it to > > throw an exception on error (the exception is thrown outside the > > context of rails and handled directly by the prototype library) > > > You can use the following library to allow you to parse your html files: > > >http://code.whytheluckystiff.net/hpricot/ > > > but of course it''ll be faster when operating on local files than > > through using open-uri. But this is an expensive operation which you > > really want to avoid if possible. > > > Adam--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---