[LRUG] [Help request] Programmatically inserting user data into js chart
Craig R Webster
craig at barkingiguana.com
Thu Jun 9 06:21:47 PDT 2016
Hi Jesse,
You probably want to output the raw JSON in your view. Rails HTML escapes it by default for safety:
http://api.rubyonrails.org/classes/ActionView/Helpers/OutputSafetyHelper.html#method-i-raw
Try something like this:
<%= raw JSON.pretty_generate @jsonish %>
Yours,
Craig | http://barkingiguana.com/
--
Barking Iguana Ltd. is a company registered in England and Wales.
Registered number: 08915147. Registered address: Jubilee House, East Beach, Lytham, St. Annes, Lancashire, England, FY8 5FT.
> On 9 Jun 2016, at 13:56, Jesse Waites <jesse.waites at gmail.com> wrote:
>
> Thanks Oto!
>
> So strange...
>
> def make_jsonish(array)
> elements = array.each_with_index.map { |num, i|
> { key: i, value: num}
> }
>
> end
>
> without json pretty generate now is:
>
> @jsonish: [{:key=>0, :value=>5}, {:key=>1, :value=>3}, {:key=>2, :value=>2}]
>
> In the page source, it is:
>
> var dataset =
>
> [{:key=>0, :value=>5}, {:key=>1, :value=>3}, {:key=>2, :value=>2}] ;
>
>
> which is the greater than sign in HTML. So weird!!!
>
> On Thu, Jun 9, 2016 at 2:20 AM, Oto Brglez <otobrglez at gmail.com <mailto:otobrglez at gmail.com>> wrote:
> Hey!
>
> Loose "JSON.pretty_generate" at the and new-lines will go away.
>
> Cheers!
>
> - Oto
>
> On Wed, Jun 8, 2016 at 11:53 PM Jesse Waites <jesse.waites at gmail.com <mailto:jesse.waites at gmail.com>> wrote:
> Update: I tried it with Tom's pretty generate JSON method and am a little closer - @jsonish is now:
>
> @jsonish: "[\n {\n \"key\": 0,\n \"value\": 5\n },\n {\n \"key\": 1,\n \"value\": 3\n },\n {\n \"key\": 2,\n \"value\": 2\n }\n]"
>
> So looks like I need to strip the new lines out? Or rather, not put them in in the first place? Its good to know I was close in my solution path.
>
> On Wed, Jun 8, 2016 at 1:03 PM, John Cinnamond <jc at panagile.com <mailto:jc at panagile.com>> wrote:
> As an aside, `map` returns an enumerator if you don’t give it a block, and enumerators have a `with_index` method. So instead of:
>
> array.each_with_index.map { |value, idx| … }
>
> You can say:
>
> array.map.with_index { |value, idx| … }
>
> This only saves you 5 characters, but I think it makes the intent slightly clearer.
>
>
> On Wed, 08 Jun 2016 at 17:57 Tom Stuart <Tom Stuart <mailto:Tom+Stuart+%3Ctom at codon.com%3E>> wrote:
> On 8 Jun 2016, at 14:32, Jesse Waites <jesse.waites at gmail.com <mailto:jesse.waites at gmail.com>> wrote:
> > def make_jsonish(array)
> > array.each_with_index do |num, i|
> > puts "{ key: #{i}, value: #{num}},"
> > end
> > end
>
> This looks pretty close to being right, but you want to return a string by joining all the individual lines, not print them out. For example:
>
> def make_jsonish(array)
> array.each_with_index.map { |num, i|
> "{ key: #{i}, value: #{num}},"
> }.join("\n")
> end
>
> Less significantly, it would be better to rely on Ruby to turn the resulting data structure (including enclosing square brackets) into JSON for you, so that you’re not responsible for the conversion yourself:
>
> def make_jsonish(array)
> elements = array.each_with_index.map { |num, i|
> { key: i, value: num}
> }
> JSON.pretty_generate(elements)
> end
>
> Cheers,
> -Tom
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org <mailto:Chat at lists.lrug.org>
> Archives: http://lists.lrug.org/pipermail/chat-lrug.org <http://lists.lrug.org/pipermail/chat-lrug.org>
> Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org <http://lists.lrug.org/options.cgi/chat-lrug.org>
> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org <http://lists.lrug.org/listinfo.cgi/chat-lrug.org>
>
>
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org <mailto:Chat at lists.lrug.org>
> Archives: http://lists.lrug.org/pipermail/chat-lrug.org <http://lists.lrug.org/pipermail/chat-lrug.org>
> Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org <http://lists.lrug.org/options.cgi/chat-lrug.org>
> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org <http://lists.lrug.org/listinfo.cgi/chat-lrug.org>
>
>
>
>
> --
> Jesse Waites
> JesseWaites.com <http://jessewaites.com/>_______________________________________________
> Chat mailing list
> Chat at lists.lrug.org <mailto:Chat at lists.lrug.org>
> Archives: http://lists.lrug.org/pipermail/chat-lrug.org <http://lists.lrug.org/pipermail/chat-lrug.org>
> Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org <http://lists.lrug.org/options.cgi/chat-lrug.org>
> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org <http://lists.lrug.org/listinfo.cgi/chat-lrug.org>
>
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org <mailto:Chat at lists.lrug.org>
> Archives: http://lists.lrug.org/pipermail/chat-lrug.org <http://lists.lrug.org/pipermail/chat-lrug.org>
> Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org <http://lists.lrug.org/options.cgi/chat-lrug.org>
> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org <http://lists.lrug.org/listinfo.cgi/chat-lrug.org>
>
>
>
>
> --
> Jesse Waites
> JesseWaites.com <http://jessewaites.com/>_______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> Archives: http://lists.lrug.org/pipermail/chat-lrug.org
> Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org
> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20160609/b1050a7c/attachment-0002.html>
More information about the Chat
mailing list