在設置這在我的routes.rb
:如何強制自定義渲染?
然後在TvShowsController
:
def dump
render :dump
end
然後,我添加了一個名爲dump.xml.builder
文件到views/tv_shows
目錄與此內容:
xml.instruct!
xml.title "Test"
當我做curl http://server/xml/xyz
我得到這個:
<?xml version="1.0" encoding="UTF-8"?>
<title>Testing</title>
和Rails服務器輸出是:
Started GET "/dump/twd" for 127.0.0.1 at 2011-11-26 23:29:28 -0500
Processing by TvShowsController#dump as */*
Parameters: {"prefix"=>"twd"}
Rendered tv_shows/dump.xml.builder (0.1ms)
Completed 200 OK in 40ms (Views: 40.0ms | ActiveRecord: 0.0ms)
問題是,當我使用Web瀏覽器來獲取XML;它說:Missing template tv_shows/dump
和導軌服務器輸出是:
Started GET "/dump/twd" for 127.0.0.1 at 2011-11-26 23:29:21 -0500
Processing by TvShowsController#dump as HTML
Parameters: {"prefix"=>"twd"}
Completed 500 Internal Server Error in 5ms
ActionView::MissingTemplate (Missing template tv_shows/dump, application/dump with {:handlers=>[:erb, :builder, :coffee], :formats=>[:html], :locale=>[:en, :en]}. Searched in:
* "/xxxxxx/ruby/pppp/app/views"
):
app/controllers/tv_shows_controller.rb:25:in `dump'
Rendered /xxxxx/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/missing_template.erb within rescues/layout (0.4ms)
據我所知,我告訴Ruby來渲染使用:dump
始終認爲...那麼,爲什麼它嘗試呈現爲HTML?
我不想回應HTML ...所以這就是爲什麼我沒有添加'respond_to'。另外,我想返回一個XML,而不管我給URL的擴展名是什麼。我的意思是,我希望'/ dump/xyz'也可以返回XML。 – Cristian
是否曾經爲此找到過解決方案? – DrewBaumann