<div dir="ltr">Hi everyone,<div><br></div><div>I spun up a new Rails app to experiment with the D3 charting library and hit a minor snag. I've never done anything like the before so I want to make sure I do it the "best" way possible and not some hacky way, which I could certainly come up with, ha.</div><div><br></div><div>Here's the situation:</div><div><br></div><div>I have a new Rails app with 1 model, view, and controller called "Favorites"</div><div>A user would add favorite colors, favorite movie, and favorite numbers, in 3 different form fields, each as a string.</div><div><br></div><div>In the controller, I remap the numbers string into  a proper array with integers.</div><div><br></div><div>@data = @favorite.numbers.split(",").map { |s| s.to_i }</div><div><br></div><div>At this point, lets say @data is now an array of [5,4,3,2,1] - We know the length of this array and the contents.</div><div><br></div><div>On the show page, in the view, I have a variable called dataset that the hardcoded </div><div>D3 chart is working off of. Here is a jsfiddle of the exact chart I am trying to get to work with user data.</div><div><br></div><div><a href="http://jsfiddle.net/enigmarm/3HL4a/13/">http://jsfiddle.net/enigmarm/3HL4a/13/</a><br></div><div><br></div><div><div><br class="">var dataset = [</div><div><span class="" style="white-space:pre">       </span>{ key: 0, value: 5 },</div><div><span class="" style="white-space:pre">      </span>{ key: 1, value: 10 },</div><div><span class="" style="white-space:pre">     </span>{ key: 2, value: 13 },</div><div><span class="" style="white-space:pre">     </span>{ key: 3, value: 19 },</div><div><span class="" style="white-space:pre">     </span>{ key: 4, value: 21 },</div><div><span class="" style="white-space:pre">     </span>{ key: 5, value: 25 },</div><div><span class="" style="white-space:pre">     </span>{ key: 6, value: 22 },</div><div><span class="" style="white-space:pre">     </span>{ key: 7, value: 18 },</div><div><span class="" style="white-space:pre">     </span>{ key: 8, value: 15 },</div><div><span class="" style="white-space:pre">     </span>{ key: 9, value: 13 },</div><div><span class="" style="white-space:pre">     </span>{ key: 10, value: 11 },</div><div><span class="" style="white-space:pre">    </span>{ key: 11, value: 12 },</div><div><span class="" style="white-space:pre">    </span>{ key: 12, value: 15 },</div><div><span class="" style="white-space:pre">    </span>{ key: 13, value: 20 },</div><div><span class="" style="white-space:pre">    </span>{ key: 14, value: 18 },</div><div><span class="" style="white-space:pre">    </span>{ key: 15, value: 17 },</div><div><span class="" style="white-space:pre">    </span>{ key: 16, value: 16 },</div><div><span class="" style="white-space:pre">    </span>{ key: 17, value: 18 },</div><div><span class="" style="white-space:pre">    </span>{ key: 18, value: 23 },</div><div><span class="" style="white-space:pre">    </span>{ key: 19, value: 25 } ];</div></div><div><br></div><div>So I need to iterate over my array, put the index into the "key" position, and put the value into the "value" section. If the array had 2 elements, I need to do it twice. 5 elements, 5 times.</div><div><br></div><div>I wrote a stupid method called "make jsonish" that isnt working:</div><div><br></div><div><div>def make_jsonish(array)</div><div>      array.each_with_index do |num, i|</div><div>        puts "{ key: #{i}, value: #{num}},"</div><div>      end</div><div>    end</div></div><div><br></div><div>I don't think the "puts" thing will work anyway. I'm a bit stumped as to how to best do this so I would appreciate any help.</div><div><br></div><div>Here is a gist of the view:</div><div><br></div><div><a href="https://gist.github.com/piratebroadcast/19b2ca6617343d8812d2a40325f76bc7">https://gist.github.com/piratebroadcast/19b2ca6617343d8812d2a40325f76bc7</a><br></div><div><br></div><div>Controller gist:</div><div><br></div><div><a href="https://gist.github.com/piratebroadcast/ce802346d75e667fface711f8dfc17e0">https://gist.github.com/piratebroadcast/ce802346d75e667fface711f8dfc17e0</a><br></div><div><br></div><div><br></div><div>Anyone have any suggestions as to how I should move forward with this? Seems like a simple thing but I can't quite sort it out by myself.</div><div><br></div><div><br></div><div>Thank you!!!</div><div><br></div><div><br></div><div><br></div><div><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div>Jesse Waites</div><div><a href="http://JesseWaites.com" target="_blank">JesseWaites.com</a></div></div></div></div></div></div>
</div></div>