Ruby hash to JavaScript

ID : 274492

viewed : 38

Tags : javascriptrubyhashjavascript





Top 3 Answer for Ruby hash to JavaScript

vote vote

93

Ruby code:

state = { 'Waiting' => { name: 'Waiting now', color: 'btn-default' }, 'Trying' => { name: 'Trying now', color: 'btn-danger' }, 'Answered' => { name: 'Answered now', color: 'btn-success' } } 

javascript code:

var state = JSON.parse('#{raw(state.to_json)}'); 
vote vote

86

Use my_awesome_ruby_hash.to_json and then you can simply either eval it in js or use parseJSON. You might need to require 'json' (not in Rails).

vote vote

80

Top 3 video Explaining Ruby hash to JavaScript







Related QUESTION?