never_had_a_name
never_had_a_name

Reputation: 93196

how to request a page in xml in ruby-on-rails?

here is my code in a controller

respond_to do |format|
  format.html
  format.xml  { render :xml => @menus }
end

when i type "http://192.168.210.136:3000" then it renders the html version.

but how do i request the xml version? i tried http://192.168.210.136:3000.xml and http://192.168.210.136:3000/index.xml with no luck.

thanks!

Upvotes: 0

Views: 712

Answers (3)

Omar Qureshi
Omar Qureshi

Reputation: 9093

Suggest you give http://guides.rubyonrails.org/routing.html#formats-and-respond-to a good read

Upvotes: 1

DBA
DBA

Reputation: 653

If you wish to return XML - by default - in your root route, you might need to change your routes.rb to make it more explicit, like this (rails 3 example):

Sandbox::Application.routes.draw do |map|
  root :to => "static#index", :format => :xml
end

On rails 2.3.x you can pass your routes a defaults hash, like this:

map.connect 'photos/:id', :controller => 'photos',
                          :action => 'show',
                          :defaults => { :format => 'jpg' }

For more information regarding the rails router, please check the official Rails guides at: http://guides.rubyonrails.org/routing.html

Upvotes: 1

bjg
bjg

Reputation: 7477

In the general case you add the .xml suffix to your URL to tell the Rails responder what you want. Your controller logic is the correct way to handle the incoming request.

E.g. A User show for ID=1 in XML would look like this:

http://192.168.210.136:3000/users/1.xml

Upvotes: 1

Related Questions