Can someone provide an example of using Builder to create multiple XML files from a SQL database. I can easily create one containing the entire database as in here...
def index
respond_to do |format|
format.xml { @rides = Rides.find(:all) }
end
end
This will create a file called index.xml based on a file I created called index.xml.builder which contains the following code
xml.instruct!
xml.rides do
@rides.each do |ride|
xml.item("togive" => ride.togive, "totake" => ride.totake, "howlong" => ride.howlong, "isoffer" => ride.isoffer, "id" => ride.id, "contact" => ride.contact)
end
end
Here are the only lines in my routes.rb
map.resources :rides
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
This works fine, but how would I create a new file that contains the last element in Rides?
Thanks
edit: changed from format.rss to format.xml
I'm not entirely sure what you're trying to achieve, but you could have another method in your controller that returns the last ride:
And then in the view template
last.xml.builderyou could have something like:If you went with this approach then you'd need to modify your
config/routes.rbfile to add a new collection route for thelistaction:Alternatively you could simply make the
findcall within the controller'sindexmethod conditional so that it fetches all rides or the last ride as appropriate. In this case you should be able to re-use the existingindex.xml.builderview template because it will just output a collection that happens to only ever contain one ride.