Vikram
Vikram

Reputation: 3351

Create json object with loop ruby on rails

I want to create a JSON object out of ruby object on view file like

:coffeescript
 files = {}
  - @filelist.each do |f|
    = files[f[0]] = f[1]
  console.log(files)

it gives me an "Unexpected logic" error

following is array declared in controller

@filelist = Array.new
@filelist.push(['ref','count'])
@filelist.push(['input1','count'])
@filelist.push(['input2','count'])

Upvotes: 1

Views: 784

Answers (1)

Sahil
Sahil

Reputation: 3358

In your view you first have to convert ruby array to json (inside view):

var filelists = $.parseJSON(<%= @filelist.to_json %>);

Then create a json object in view:

 var jsonObj = {};
    $.each(filelists, function(obj,index){
      jsonObj[obj[0]] = obj[1];
    });

Upvotes: 1

Related Questions