Topic: Read ruby hashes in javascript

Hi,

Does anybody know how to read hashes in javascript? I got return value undefined. Any tutorials are really appreciated.

Thank you!

Re: Read ruby hashes in javascript

You would have to output the ruby hash and make it into a javascript hash...

# helper_method

def hash_for_js(ruby_hash)
  js_params = ruby_hash.map {|k,v| "'#{k}': '#{v}'"}.join(",")
  "$H({#{js_params})"
end
<%= javascript_tag "doSomethingWithAHash(#{hash_for_js(@my_ruby_hash)})" %>

-patrick

Last edited by patrick99e99 (2009-08-30 01:12:19)