Today's the day

向软件大牛炫耀我会焊单片机,向硬件大牛炫耀我会写 Rails,向软硬件大牛炫耀我生物,向软硬件生物大牛炫耀我会折腾期货 -_-bbb

迂回的实现了覆盖 helper 中的函数
rake rails:update

Ruby 数组转换成 Javascript 参数

galeki posted @ 2008年3月06日 12:14 in RoR with tags ruby javascript , 9219 阅读

有的时候数据存储在 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}

Rails 中的 Helper 也用这俩函数生成 Javascript 的参数。

inspect 只适合偷懒用,毕竟 inspect 的格式并不保证不会变,也许 Ruby 下一般换个更漂亮的方式显示 inspect 结果,这招就不灵了。

Digital_Work 说:
2020年10月29日 20:33

Friend, this web site might be fabolous, i just like it. hasta karyolası

Digital_Work 说:
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ğı

Digital_Work 说:
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

Digital_Work 说:
2020年10月29日 21:11

howdy, your websites are really good. I appreciate your work. marmaris rodos feribot

Digital_Work 说:
2020年10月29日 21:31

Nice post mate, keep up the great work, just shared this with my friendz hasta yatağı

Digital_Work 说:
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

Digital_Work 说:
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

Digital_Work 说:
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

Digital_Work 说:
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

Digital_Work 说:
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ı

Digital_Work 说:
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

Digital_Work 说:
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

Digital_Work 说:
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

Digital_Work 说:
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

Digital_Work 说:
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/

Digital_Work 说:
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

Digital_Work 说:
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

Digital_Work 说:
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/

Hammad Amir 说:
2021年9月18日 23:22

If you are looking for more information about flat rate locksmith Las Vegas check that right away. บุหรี่ไฟฟ้า


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter