我不知道最好的做法是什麼,但在我的情況下,我在模型中做1,2,和3一類的方法。我會創建一個模型方法,它接受基本XML文件的名稱。
所以我會做這樣的事情...(警告:我不是100%確定這是語法正確的,我有點新的Rails仍然)
# app/models/mymodel.rb
class MyModel < ActiveRecord::Base
# ...
def self.formatXml(xml_base)
dataRecords = MyModel.find(:whatever) # step 1
# open the XML and read it into a string, this is step 2
# take the XML string, do the appropriate gsubs
return my_xml_string;
end
end
# app/controllers/mycontroller.rb
class MyController < ActionController::Base
# ...
def myAction
formattedXml = MyModel.formatXml(whateverXmlFileName)
render :text => formattedXml
end
end
由於這看起來好 – user448157 2010-09-23 15:50:40