1

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'])
1
  • Your indentation is all over the place. The first step is to fix it. Commented Dec 18, 2015 at 6:14

1 Answer 1

1

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];
    });
Sign up to request clarification or add additional context in comments.

2 Comments

it seems to be working except I want it in haml file. tried following but its not working - #{@filelist.to_json}
I have not worked using HAML files, but after looking at some tutorials I guess in HAML you would have to use this: [email protected]_json.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.