What is the best way to return multiple variables and arrays formatted in xml, json etc with respond_with

api, ruby, ruby-on-rails, ruby-on-rails-3

Solution

Instead of merging @ad, @map, just create create a new hash then put add the arrays to it, like

respond_with({:ad => @ad, :map => @map, :article => @article})

It just render all the datas with groups.

Problem

I'm looking for a good way to return xml or json from a controller including multiple variables. For example: ``` def index @ad = Ad.some_annoying_ad @map = Map.some_map_for_something @articles = Articles.trending respond_with @articles end ``` How would I best add the @ad and @map var to the @articles array? I have seen people using the merge function, but I am not sure if that's what I'm looking for. Just want to know which way is most standard, flexible, DRY. Thanks! Note: I am aware that respond with will automatically format the results in xml or json depending on the file extension added to the url. Thanks!

Original source