在设置这在我的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