[LRUG] [Help request] Programmatically inserting user data into js chart

Mark Burns markthedeveloper at gmail.com
Thu Jun 9 18:38:26 PDT 2016


Note that using raw potentially opens you up to XSS attacks. Although, it
depends on how you validate/coerce your data

On Thu, Jun 9, 2016 at 10:28 PM Jesse Waites <jesse.waites at gmail.com> wrote:

> That did it!!!
>
> <%= raw JSON.pretty_generate @jsonish %> has it working perfectly. Thanks
> so much everyone for your help with this!
>
> On Thu, Jun 9, 2016 at 9:21 AM, Craig R Webster <craig at barkingiguana.com>
> wrote:
>
>> 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> 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>
>>> 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> 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
>>>>> <Tom+Stuart+%3Ctom at codon.com%3E>> wrote:
>>>>>
>>>>>> On 8 Jun 2016, at 14:32, Jesse Waites <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
>>>>>> 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
>>>>>>
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> 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
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> 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
>>>>
>>>
>>> _______________________________________________
>>> 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
>>>
>>>
>>
>>
>> --
>> 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
>>
>>
>>
>> _______________________________________________
>> 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
>>
>>
>
>
> --
> Jesse Waites
> 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/20160610/4efaac05/attachment-0002.html>


More information about the Chat mailing list