在 Ruby on Rails 上构建 Javascript 对象数组的最佳方法
我有这个,但我很确定这不是构建它的最佳方法。
var dogs = {
'names' : ["a", "b", "c"],
'images': [
<% @dogs.images.each do |image| %>
{
'thumb' : '<%= image.thumb %>',
'medium' : '<%= image.medium %>',
} <%= "," unless(@dogs.images.last.id == image.id) %>
<% end %>
]
}
感谢您的帮助!
I have this but I'm pretty sure that is not the best way to build it up.
var dogs = {
'names' : ["a", "b", "c"],
'images': [
<% @dogs.images.each do |image| %>
{
'thumb' : '<%= image.thumb %>',
'medium' : '<%= image.medium %>',
} <%= "," unless(@dogs.images.last.id == image.id) %>
<% end %>
]
}
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不直接将图像转换为 json 对象,例如:
您还可以覆盖模型中的 to_json 方法以排除您不想显示的某些方法。
您可以在此处查看如何覆盖 to_json:
Why don't use just convert the images to a json object like:
You could also overwrite the to_json method in your model to exclude certain methods you don't want to display.
You can see how to overwrite to_json here:
http://www.theirishpenguin.com/2008/05/19/quick-example-of-serialisation-via-to_json-in-ruby-on-rails/