我正在学习Rails,我正在尝试获取更新ActiveRecord的XML输出。这是我的routes.rb
:
Sparklizer::Application.routes.draw do
resources :updates
get "updates/new"
get "updates/list"
get "home/index"
match "updates/xml/:id" => "updates#xmlOut"
end
match "updates/xml/:id" => "updates#xmlOut"
是我尝试输出XML的地方。这是xmlOut
方法:
def xmlOut
render :layout => false
headers['Content-Type'] = 'application/xml'
@xml = Builder::XmlMarkup.new
@update = Update.find(params[:id])
xml.instruct! :xml, :version=> "1.0"
xml.declare! :DOCTYPE, :html, :PUBLIC, "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
xml.update {
for data in @update
xml.data do
xml.title(data.title)
xml.description(data.description)
end
end
}
end
运行并访问/updates/xml/2
时,我得到了
Template is missing
Missing template updates/xmlOut, application/xmlOut with {:handlers=>[:erb, :builder, :coffee], :formats=>[:html], :locale=>[:en, :en]}. Searched in: * "/Users/pablo/Documents/Workspace/Sparklizer/app/views"
我做错了什么?谢谢!
答案 0 :(得分:0)
您正在尝试在没有视图文件的情况下呈现xml。为了使它工作,你必须渲染你的xml'内联'。来自docs:
render :inline => "xml.p {'Horrid coding practice!'}", :type => :builder
但是渲染内联被认为是一种不好的做法。
所以你应该更好地使用respond_with结构。这样的事情(根据需要调整):
def xmlOut
@update = Update.find(params[:id])
respond_with(@update) do |format|
format.xml { render :xml => @update }
end
end
还有一个railscast涵盖了这个。