Ruby hash to JavaScript
I have a Ruby hash which is passed to a hidden field. How do I extract this hash into JavaScript arrays that开发者_如何学C I can work with? I need to access the key/value pairs in JavaScript.
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)}');
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).
 加载中,请稍侯......
      
精彩评论