2016-05-12 33 views
0

我使用axlsx宝石生成Excel电子表格。我试图将生成的电子表格发送到模型进行压缩。此方法将excel文件与其他一些文件进行压缩。的Rails:发送由axlsx视图生成的文件到模型

在我的模型的方法是这样的:

def zipper 
    tempfile = Tempfile.new 
    children = self.children_with_forms 

    Zip::OutputStream.open(tempfile) do |stream| 
    children.each do |child| 
     directory = "#{child.wide_reference[0,3]}/" 

     if child.model_name == "Position" 
     stream.put_next_entry("#{child.volume} #{child.title} TOC.xlsx") 
     stream.print IO.read(Rails.application.routes.url_helpers.toc_path(format: :xlsx, position_id: child.id)) 
     end 

     stream.put_next_entry("#{directory}#{child.wide_reference}-#{child.short_name}-#{child.title.truncate(15, omission:'')}.docx") 
     stream.print IO.read(child.download_form.path) 
    end 
    end 

    tempfile 
end 

我有问题的部分是:

if child.model_name == "Position" 
    stream.put_next_entry("#{child.volume} #{child.title} TOC.xlsx") 
    stream.print IO.read(Rails.application.routes.url_helpers.toc_path(format: :xlsx, position_id: child.id)) 
    end 

我如何生成的文件将模型?

+0

agustaf,你应该在这里发表您的最终解决方案,并标记您自己的答案是正确的。 – noel

回答

0

我最后不得不使用模型内部渲染视图:ActionView::Base.new(ActionController::Base.view_paths, {key: value}),感谢帮助我收到here

下面是结束了工作。

def download 
    tempfile = Tempfile.new 
    children = self.children_with_forms 
    Zip::OutputStream.open(tempfile) do |stream| 
    children.each do |child| 
     directory = "#{child.wide_reference[0,3]}/" 
     if child.model_name == "Position" 
     av = ActionView::Base.new(ActionController::Base.view_paths, {position: child, model: child.model}) 
     stream.put_next_entry("#{directory}#{child.volume} #{child.title} TOC.xlsx") 
     @position = child 
     @model = child.model 
     stream.print av.render template: 'pages/toc.xlsx.axlsx' 
     end 
     stream.put_next_entry("#{directory}#{child.wide_reference} #{child.title.truncate(15, omission:'')} (#{child.short_name}).docx") 
     stream.print IO.read(child.download_form.path) 
    end 
    stream.put_next_entry("Excel File.xlsx") 
    av = ActionView::Base.new(ActionController::Base.view_paths, {model: self}) 
    stream.print av.render template: 'pages/excel_file.xlsx.axlsx' 
    end 
    tempfile 
end 

注:“范式”是类,这种方法是在名称

相关问题