Ruby on rails 从rake任务渲染视图(带有部分视图)
我在rake任务中获得了以下代码:Ruby on rails 从rake任务渲染视图(带有部分视图),ruby-on-rails,erb,Ruby On Rails,Erb,我在rake任务中获得了以下代码: class PdfExporter < ActionView::Base include Rails.application.routes.url_helpers include ActionView::Helpers::TagHelper include ActionView::Helpers::UrlHelper def generate_pdf_from_html content = File.read('path/t
class PdfExporter < ActionView::Base
include Rails.application.routes.url_helpers
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper
def generate_pdf_from_html
content = File.read('path/to/view.html.erb')
erb = ERB.new(content)
html_content = erb.result(binding)
# ... some pdf stuff
end
end
是空的。这可能意味着ActionView“不知道”在哪里搜索视图。下面是我的一个rake任务示例,它从视图创建html文件。为了让它工作,您必须通过覆盖一些默认值并传入一个假控制器和请求来安抚ActionView:
desc 'Example of writing a view to a file'
task :example => :environment do
# View that works with Rake
class RakeActionView < ActionView::Base
include Rails.application.routes.url_helpers
include ::ApplicationHelper
# Include other helpers that you will use
# Make sure this matches the expected environment, e.g. localhost for dev
# and full domain for prod
def default_url_options
{host: 'localhost:3000'}
end
# It is safe to assume that the rake request is legit
def protect_against_forgery?
false
end
end
# build a simple controller to process the view
controller = ActionController::Base.new
controller.class_eval do
include Rails.application.routes.url_helpers
include ::ApplicationHelper
end
# build a fake request
controller.request = ActionDispatch::TestRequest.new
# build the rake view with the path to the app views
view = RakeActionView.new(Rails.root.join('app', 'views'), {}, controller)
# example assigning instance @variables to the view
view.assign( :user => User.first )
# Render the view to html
html = view.render(template: 'relative/path/to/template', layout: 'layouts/example')
# Write html to temp file and then copy to destination
temp = Tempfile.new('example_file')
temp.write(html)
FileUtils.cp( temp.path, 'path/to/exmple_file' )
# optionally set permissions on file
File.chmod(0774, 'path/to/exmple_file')
# close up the temp file
temp.close
temp.unlink
end
desc'将视图写入文件的示例'
任务:示例=>:环境do
#使用Rake的视图
类RakeActionViewuser.first)
#将视图呈现为html
html=view.render(模板:'relative/path/to/template',布局:'layouts/example')
#将html写入临时文件,然后复制到目标
temp=Tempfile.new('example_file')
临时写入(html)
cp(temp.path,“path/to/example\u文件”)
#可以选择设置文件的权限
chmod(0774,'path/to/example\u File')
#关闭临时文件
临时关闭
临时取消链接
结束
看起来您遇到的问题已由
RakeActionView.new(Rails.root.join('app','views'),{},controller)
修复,它设置了ActionView查找模板的路径。您是否尝试过绝对路径,如
?
Searched in:
desc 'Example of writing a view to a file'
task :example => :environment do
# View that works with Rake
class RakeActionView < ActionView::Base
include Rails.application.routes.url_helpers
include ::ApplicationHelper
# Include other helpers that you will use
# Make sure this matches the expected environment, e.g. localhost for dev
# and full domain for prod
def default_url_options
{host: 'localhost:3000'}
end
# It is safe to assume that the rake request is legit
def protect_against_forgery?
false
end
end
# build a simple controller to process the view
controller = ActionController::Base.new
controller.class_eval do
include Rails.application.routes.url_helpers
include ::ApplicationHelper
end
# build a fake request
controller.request = ActionDispatch::TestRequest.new
# build the rake view with the path to the app views
view = RakeActionView.new(Rails.root.join('app', 'views'), {}, controller)
# example assigning instance @variables to the view
view.assign( :user => User.first )
# Render the view to html
html = view.render(template: 'relative/path/to/template', layout: 'layouts/example')
# Write html to temp file and then copy to destination
temp = Tempfile.new('example_file')
temp.write(html)
FileUtils.cp( temp.path, 'path/to/exmple_file' )
# optionally set permissions on file
File.chmod(0774, 'path/to/exmple_file')
# close up the temp file
temp.close
temp.unlink
end