Ruby 数组转换成 Javascript 参数
有的时候数据存储在 Ruby 的数组里,如果你想把这个数组当作 Javascript 的函数的参数,比如:
@names = ['Zhangsan','Lisi']
你想把他作为参数传给 Javascript 代码,达到这样的效果:
a_js_function(['Zhangsan','Lisi']);
需要把 @names 转换成字符串的表达形式,你可能想到迭代数组中的每项,拼出最终的字符串,其实简单的方法是 inspect:
a_js_function(<%= @name.inspect %>);
因为在 Ruby 和 Javascript 中数组的表达是基本一样的,所以这样刚好达到了要求。
不过如果数组中有不期望的数据,比如 nil 或者一个复杂的对象什么的,结果可能就不是你希望的了。那么可以试试 array_or_string_for_javascript:
a_js_function(<%= array_or_string_for_javascript(@names) %>);
不过,array_or_string_for_javascript 会把数组中所有的项目都转换成字符串,所以如果数组是 [100, 200, 300] 就会被转换成 ['100', '200', '300'] 如果你想传入数值的话就用前面的 inspect 了。
另外还有一个 options_for_javascript,把 Ruby 的 Hash 转成 Javascript 的格式:
<%= options_for_javascript(:a => 1, :b => 2, :c => 3) %>
=> {a:1, b:2, c:3}
=> {a:1, b:2, c:3}
Rails 中的 Helper 也用这俩函数生成 Javascript 的参数。
inspect 只适合偷懒用,毕竟 inspect 的格式并不保证不会变,也许 Ruby 下一般换个更漂亮的方式显示 inspect 结果,这招就不灵了。
2020年10月29日 20:33
Friend, this web site might be fabolous, i just like it. hasta karyolası
2020年10月29日 20:53
Nice post. I was checking constantly this blog and I am impressed! Extremely helpful information specially the last part I care for such info a lot. I was seeking this particular information for a very long time. Thank you and good luck. hasta yatağı
2020年10月29日 21:00
Nice post. I was checking constantly this blog and I am impressed! Extremely helpful information specially the last part I care for such info a lot. I was seeking this particular information for a very long time. Thank you and good luck. en uygun uçak bileti
2020年10月29日 21:11
howdy, your websites are really good. I appreciate your work. marmaris rodos feribot
2020年10月29日 21:31
Nice post mate, keep up the great work, just shared this with my friendz hasta yatağı
2020年10月29日 21:44
Thanks so much for sharing this awesome info! I am looking forward to see more postsby you! Turkey plastic surgery
2020年10月29日 21:50
Wow, cool post. I’d like to write like this too – taking time and real hard work to make a great article… but I put things off too much and never seem to get started. Thanks though. dış cephe temizlik
2020年10月29日 22:10
Thanks for your insight for your fantastic posting. I’m glad I have taken the time to see this. pubg mobile emülatör hile
2020年10月29日 22:16
This is the type of information I’ve long been trying to find. Thank you for writing this information. elektronik sigara
2020年10月29日 22:22
Fantastic blog! Do you have any tips and hints for aspiring writers? I’m planning to start my own website soon but I’m a little lost on everything. Would you propose starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m completely overwhelmed .. Any suggestions? Many thanks! hasta karyolası
2020年11月09日 20:59
I have recently started a blog, the info you provide on this site has helped me greatly. Thanks for all of your time & work. Zurapedia
2020年11月11日 18:44
Nice post. I was checking constantly this blog and I am impressed! Extremely helpful information specially the last part I care for such info a lot. I was seeking this particular information for a very long time. Thank you and good luck. https://apnews.com/press-release/ts-newswire/technology-business-cryptocurrency-financial-technology-south-africa-ee038bb5ba004fc99868d5db1e337b1c
2020年11月11日 18:51
I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. bitcoin revolution south africa patrice motsepe
2020年11月11日 18:57
Thanks so much for sharing this awesome info! I am looking forward to see more postsby you! bitcoin revolution patrice motsepe
2020年11月11日 19:04
Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. https://apnews.com/press-release/newmediawire/1236c37187161b7cc67e570257716b86/
2020年11月11日 19:10
I’ve read some good stuff here. Definitely worth bookmarking for revisiting. I surprise how much effort you put to create such a great informative website. bitcoin revolution south africa
2020年11月11日 19:15
Thankyou for this wondrous post, I am glad I observed this website on yahoo. bitcoin revolution south africa patrice motsepe
2020年11月11日 19:19
Wonderful blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Appreciate it. http://www.ladieswholunch.co.za/
2021年9月18日 23:22
If you are looking for more information about flat rate locksmith Las Vegas check that right away. บุหรี่ไฟฟ้า