views:

38

answers:

1

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!

+5  A: 

Why don't use just convert the images to a json object like:

var dogs = {
  'names' : ["a", "b", "c"],
  'images': <%= @dogs.images.to_json %>
}

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/

Max Schulze