ruby-on-rails - 如何将参数传递到xml文件中?
问题描述
我有这样的工作:jobs/create_site_map.rb
class CreateSiteMap
@queue = :create_site_map
def self.perform(slct_common_path, http_host, domain_name)
... some code ...
q_jobs = TJob.q_job_site_map(m_site.id)
q_jobs.each do |job|
site_map_content =
"\n\t\t<url>
\t\t<loc>#{site_frontsiteurl}index.cfm?fuseaction=job.detail&sgtno=#{job.shigoto_no}</loc>
\t\t<lastmod>#{job.job_dt.strftime("%F")}</lastmod>
\t\t<changefreq>daily</changefreq>
\t\t<priority>0.8</priority>
\t\t</url>\n"
sitemap_data << site_map_content
end
end
end
我想将上面的“site_map_content”变量带到.xml文件(模板)中以重复使用多次,例如:
<url>
<loc>#{site_frontsiteurl}index.cfm?fuseaction=job.detail&sgtno=#{job.shigoto_no}</loc>
<lastmod>#{job.job_dt.strftime("%F")}</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
但是如何将参数传递到 xl 文件中?请帮助我。
解决方案
像这样使用 Erb 创建一个模板:-
template = Erb.new(File.read('file_path')) #这是一个yml文件
然后将数据传递到模板中:
struct = OpenStruct.new(data)
YAML.load template.result(
struct.instance_eval { binding }
)
稍后您可以将 yml 转换为 xml。