Tom*_*man 10 rake ruby-on-rails
我想使用Rake任务来缓存我的站点地图,以便请求sitemap.xml
不会永远.这是我到目前为止所拥有的:
@posts = Post.all
sitemap = render_to_string :template => 'sitemap/sitemap', :locals => {:posts => @posts}, :layout => false
Rails.cache.write('sitemap', sitemap)
Run Code Online (Sandbox Code Playgroud)
但是当我尝试运行它时,我收到一个错误:
undefined local variable or method `headers' for #<Object:0x100177298>
Run Code Online (Sandbox Code Playgroud)
如何从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)
Run Code Online (Sandbox Code Playgroud)
请注意,我将模板转换为部分模板以使其工作