开发者

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!


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜