2010-07-17 71 views
0

這是我在控制器如何在ruby-on-rails的xml中請求一個頁面?

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

代碼當我鍵入「http://192.168.210.136:3000」,那麼它呈現HTML版本。

但我該如何請求xml版本?我試過http://192.168.210.136:3000.xmlhttp://192.168.210.136:3000/index.xml沒有運氣。

謝謝!

+0

沒有測試猜測:http://192.168.210.136: 3000/.xml – Joni 2010-07-17 19:56:12

+0

你的map.root在你的路線文件中看起來像什麼? – Gareth 2010-07-17 19:59:26

+0

@gareth:root:to =>'firstpage#index' – 2010-07-17 21:27:33

回答

1

如果您希望返回XML - 默認情況下 - 在你的根路徑,您可能需要改變你的路線。 RB,使之更加明確,像這樣的(軌道3例):

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

on Rails的2.3.x版本,你可以通過你的路由默認一個哈希值,像這樣:

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

有關軌道路由器的更多信息,請查看官方的Rails指南在: http://guides.rubyonrails.org/routing.html

1

在一般情況下,您將.xml後綴添加到您的URL以告訴Rails響應者您想要什麼。您的控制器邏輯是處理傳入請求的正確方法。

E.g. XML中的Usershow爲ID = 1是這樣的:

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

相關問題