首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何延迟渲染作业?

如何延迟渲染作业?
EN

Stack Overflow用户
提问于 2011-09-18 21:36:52
回答 1查看 720关注 0票数 2

这个方法运行正常,但是如果我添加delayed_job的handle_asychronously,我会得到can't convert nil into String

代码语言:javascript
复制
  def onixtwo 
        s = render_to_string(:template=>"isbns/onix.xml.builder") 
        send_data(s, :type=>"text/xml",:filename => "onix2.1.xml")
  end
     handle_asynchronously :onixtwo

因此,使用延迟作业进行渲染显然存在传递参数的问题。我试过把这个任务放到一个rake任务中,但是render_to_string是一个控制器动作--而且我使用了一个current_user变量,它只需要在控制器或视图中引用。所以..。延迟渲染作业的最佳方法是什么?

/更新/

考虑到我目前正在和一个蹒跚学步的孩子进行结对编程,我没有空闲的时间来研究评论中明智地推荐的其他类方法-所以作为一个快速而棘手的尝试,我尝试了一下:

代码语言:javascript
复制
  def onixtwo 

   system "  s = render_to_string(:template=>'isbns/onix.xml.builder') ; send_data(s, :type=>'text/xml',:filename => 'onix2.1.xml') & "
   redirect_to isbns_path, :target => "_blank", :flash => { :success => "ONIX message being generated in the background." }

end

为什么不动呢?没有错误消息,也没有生成任何文件-这就是我删除系统时的情况... &

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-16 04:46:23

无论如何,这就是我所做的,完全绕过了render_to_stream。这是在/lib或app/classes中(将config.autoload_paths += %W(#{config.root}/classes添加到配置/应用程序.rb中):

#classes/bookreport.rb

代码语言:javascript
复制
# -*- encoding : utf-8 -*-
require 'delayed_job'
require 'delayed/tasks'

class Bookreport

  # This method takes args from the book report controller. First it sets the current period. Then it sorts out which report wants calling. Then it sends on the arguments to the correct class. 
  def initialize(method_name, client, user, books)
    current_period  = Period.where(["currentperiod = ? and client_id = ?", true, client]).first
    get_class       = method_name.capitalize.constantize.new
    get_class.send(method_name.to_sym, client, user, books.to_a, current_period.enddate)
  end
end

#app/classes/onixtwo.rb

代码语言:javascript
复制
require 'builder'

class Onixtwo

  def onixtwo(client_id, user_id, books, enddate)

  report_name       = "#{Client.find_by_id(client_id).client_name}-#{Time.now}-onix-21"
  filename          = "#{Rails.root}/public/#{report_name}.xml"
  current_company   = Company.where(:client_id => client_id).first

  File.open(filename, "w") do |file|

    xml = ::Builder::XmlMarkup.new(:target => file, :indent => 2)
    xml.instruct!(:xml, :version => "1.0", :encoding => "utf-8")
    xml.declare! :DOCTYPE, :ONIXMessage, :SYSTEM, "http://www.editeur.org/onix/2.1/03/reference/onix-international.dtd"
    xml.ONIXMessage do
      xml.Header do
       #masses of Builder code goes here...
    end
  end #of file
  xmlfile = File.open(filename, "r")
  onx     = Onixarchive.new(:client_id => client_id, :user_id => user_id)
  onx.xml = xmlfile
  onx.save!

end #of method
handle_asynchronously :onixtwo

end #of class

从视图调用,如下所示:

代码语言:javascript
复制
    = link_to("Export to ONIX 2.1", params.merge({:controller=>"bookreports"  , :action=>:new, :method_name => "onixtwo"}))

通过这样的控制器:

代码语言:javascript
复制
class Books::BookreportsController < ApplicationController
#uses Ransack for search, hence the @q variable
  def new 
    @q              = Book.search(params[:q])
    @books          = @q.result.order('pub_date DESC')      
    method_name     = params[:method_name]
    Bookreport.new(method_name, @client, @user, @books)
    redirect_to books_path, :flash => {:success => t("#{method_name.to_sym}").html_safe}
  end

end 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7461812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档