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!

3
  • Have a look: simonecarletti.com/blog/2010/04/… I'm too rusty to have the syntax correct here :) Commented Oct 26, 2010 at 1:58
  • what is @dogs, is it a model object? Commented Oct 26, 2010 at 4:57
  • yes, it is an active record model. Commented Oct 26, 2010 at 13:58

1 Answer 1

6

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.