Am i a total idiot to try and parse out a rails object for javascript in this way? <% for i in interface_items[0].attributes %> var <%= i[0] %> = "<%=h( i[1] )%>" <% end %> Am I missing something really obvious? Thanks, Mark -- -------------------------------------------------------------------- I am Mark Daggett and I approve this message.
A nicer way to do this would be to turn your object into JSON with ruby-json: gem install ruby-json JSON is actually valid javascript so you can then do something like this: <% require_gem ''ruby-json'' %> var data = <%= interface_items.to_json %>; then the javascript varaible data would contain a representation of the object which looks like it would be an array in this case. Cheers, Dan On 2/10/06, M Daggett <heavysixer@gmail.com> wrote:> Am i a total idiot to try and parse out a rails object for javascript > in this way? > > > <% > for i in interface_items[0].attributes > %> > var <%= i[0] %> = "<%=h( i[1] )%>" > <% > end > %> > > Am I missing something really obvious? > > Thanks, > Mark > > -- > -------------------------------------------------------------------- > I am Mark Daggett and I approve this message. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Dan Webb http://www.danwebb.net
Gregory Seidman
2006-Feb-10 13:47 UTC
[Rails] convert rails object to javascript variables
On Thu, Feb 09, 2006 at 10:16:32PM -0800, M Daggett wrote: } Am i a total idiot to try and parse out a rails object for javascript } in this way? } } <% } for i in interface_items[0].attributes } %> } var <%= i[0] %> = "<%=h( i[1] )%>" } <% } end } %> That should probably work, but you are also probably better off with JSON <http://www.json.org/>: module IncludeThisInYourHelper def obj_to_simple_JSON(obj) q = ''"'' json = ''{ '' for i in obj.attributes #escape single quotes; a </script> tag would also be bad value = i[1].to_s.gsub(/''/, "''+#{q}''#{q}+''") json << "''#{i[0]} : ''#{value}''" end return json.sub(/, $/, '' }'') end end ...and in your .rhtml... var myobject = <%= obj_to_simple_JSON %>; } Am I missing something really obvious? Just that it''s better to put such things in helpers, usually. } Thanks, } Mark --Greg
Gregory Seidman
2006-Feb-10 14:34 UTC
[Rails] convert rails object to javascript variables
On Fri, Feb 10, 2006 at 01:41:44PM +0000, Dan Webb wrote: } A nicer way to do this would be to turn your object into JSON with ruby-json: } } gem install ruby-json } } JSON is actually valid javascript so you can then do something like this: } <% } require_gem ''ruby-json'' } %> } } var data = <%= interface_items.to_json %>; } } then the javascript varaible data would contain a representation of } the object which looks like it would be an array in this case. Oh, COOL! I thought I was being spiffy with my quickie JSON output, but that''s much better. Thanks for bringing it to my (our) attention. } Cheers, } Dan --Greg
It works straight out of the box on hashes and arrays but if you want to serialize a whole object you need to make your own to_json method but its not difficult. On 2/10/06, Gregory Seidman <gsslist+ror@anthropohedron.net> wrote:> On Fri, Feb 10, 2006 at 01:41:44PM +0000, Dan Webb wrote: > } A nicer way to do this would be to turn your object into JSON with ruby-json: > } > } gem install ruby-json > } > } JSON is actually valid javascript so you can then do something like this: > } <% > } require_gem ''ruby-json'' > } %> > } > } var data = <%= interface_items.to_json %>; > } > } then the javascript varaible data would contain a representation of > } the object which looks like it would be an array in this case. > > Oh, COOL! I thought I was being spiffy with my quickie JSON output, but > that''s much better. Thanks for bringing it to my (our) attention. > > } Cheers, > } Dan > --Greg > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Dan Webb http://www.danwebb.net
Hey Dan and Gregory, That was a great help. I was able to get access to my variables 20 seconds after I installed the GEM. Thanks so much for pointing me in the right direction. Mark On 2/10/06, Dan Webb <dan@danwebb.net> wrote:> It works straight out of the box on hashes and arrays but if you want > to serialize a whole object you need to make your own to_json method > but its not difficult. > > On 2/10/06, Gregory Seidman <gsslist+ror@anthropohedron.net> wrote: > > On Fri, Feb 10, 2006 at 01:41:44PM +0000, Dan Webb wrote: > > } A nicer way to do this would be to turn your object into JSON with ruby-json: > > } > > } gem install ruby-json > > } > > } JSON is actually valid javascript so you can then do something like this: > > } <% > > } require_gem ''ruby-json'' > > } %> > > } > > } var data = <%= interface_items.to_json %>; > > } > > } then the javascript varaible data would contain a representation of > > } the object which looks like it would be an array in this case. > > > > Oh, COOL! I thought I was being spiffy with my quickie JSON output, but > > that''s much better. Thanks for bringing it to my (our) attention. > > > > } Cheers, > > } Dan > > --Greg > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > -- > Dan Webb > http://www.danwebb.net > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- -------------------------------------------------------------------- I am Mark Daggett and I approve this message.
Mark Reginald James
2006-Feb-11 09:18 UTC
[Rails] Re: convert rails object to javascript variables
Dan Webb wrote:> It works straight out of the box on hashes and arrays but if you want > to serialize a whole object you need to make your own to_json method > but its not difficult.There''s this AR->JSON function: http://www.bigbold.com/snippets/posts/show/474 -- We develop, watch us RoR, in numbers too big to ignore.
On 2/10/06, Mark Reginald James <mrj@bigpond.net.au> wrote:> Dan Webb wrote: > > It works straight out of the box on hashes and arrays but if you want > > to serialize a whole object you need to make your own to_json method > > but its not difficult. > > There''s this AR->JSON function: http://www.bigbold.com/snippets/posts/show/474Extremely useful, as is the rest of this thread. I''ve been looking all day - enough time to write a few JSON generators - for the easiest way of turning some AR objects or simple hashes into simple JavaScript (JSON). Cheers to everyone who has carried solution into this thread, - Rowan Rodrik -- Morality is usually taught by the immoral.