我想使用 Rake 任务来缓存我的站点地图,以便请求sitemap.xml
不会花很长时间。这是我到目前为止所拥有的:
@posts = Post.all
sitemap = render_to_string :template => 'sitemap/sitemap', :locals => {:posts => @posts}, :layout => false
Rails.cache.write('sitemap', sitemap)
但是当我尝试运行它时,出现错误:
undefined local variable or method `headers' for #<Object:0x100177298>
如何从 Rake 中将模板渲染为字符串?
我是这样做的:
av = ActionView::Base.new(Rails::Configuration.new.view_path)
av.class_eval do
include ApplicationHelper
end
include ActionController::UrlWriter
default_url_options[:host] = 'mysite.com'
posts = Post.all
sitemap = av.render 'sitemap/sitemap', :posts => posts
Rails.cache.write('sitemap', sitemap)
请注意,我将模板转换为部分模板以使其正常工作
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)