繁体   English   中英

response_to和服务对象

[英]respond_to and service objects

我目前正在使用docx_replace gem自动将数据插入到一组文档中。 宝石非常简单明了。 基本上,它在rails控制器内以一种特殊的方法运行,如下所示(从文档中引用):

def user_report
  @user = User.find(params[:user_id])

  respond_to do |format|
    format.docx do
      # Initialize DocxReplace with your template
      doc = DocxReplace::Doc.new("#{Rails.root}/lib/docx_templates/my_template.docx", "#{Rails.root}/tmp")

      # Replace some variables. $var$ convention is used here, but not required.
      doc.replace("$first_name$", @user.first_name)
      doc.replace("$last_name$", @user.last_name)
      doc.replace("$user_bio$", @user.bio)

      # Write the document back to a temporary file
      tmp_file = Tempfile.new('word_tempate', "#{Rails.root}/tmp")
      doc.commit(tmp_file.path)

      # Respond to the request by sending the temp file
      send_file tmp_file.path, filename: "user_#{@user.id}_report.docx", disposition: 'attachment'
    end
  end
end

但是,这使我的控制器controller肿了,因此我试图将其放入服务对象中(如下例所示):

class UserReportService
    def initialize(user)
        @user=user
    end

    def user_report_generate
        respond_to do |format|
            format.docx do
                # Initialize DocxReplace with your template
                doc = DocxReplace::Doc.new("#{Rails.root}/lib/docx_templates/my_template.docx", "#{Rails.root}/tmp")

                # Replace some variables. $var$ convention is used here, but not required.
                doc.replace("$first_name$", @user.first_name)
                doc.replace("$last_name$", @user.last_name)
                doc.replace("$user_bio$", @user.bio)

                # Write the document back to a temporary file
                tmp_file = Tempfile.new('word_tempate', "#{Rails.root}/tmp")
                doc.commit(tmp_file.path)

                # Respond to the request by sending the temp file
                send_file tmp_file.path, filename: "user_#{@user.id}_report.docx", disposition: 'attachment'
            end
        end
    end
end

并在我的控制器中执行了以下操作:

def user_report
  UserReportService.new(@user).user_report_generate
end

但是,当我调用controller方法时,出现以下错误:

17:58:10 web.1  | NoMethodError (undefined method `respond_to' for #<UserReportService:0x000000041e5ab0>):
17:58:10 web.1  |   app/services/user_report_service.rb:17:in `user_report_generate'
17:58:10 web.1  |   app/controllers/user_controller.rb:77:in `user_report'

我阅读了response_to,如果我正确地理解了文档,那么这是控制器专用的方法(这可以解释问题)。 我怎么能解决这个问题?

respond_tosend_file应该保留在您的控制器中,但是其余的逻辑可以移到服务对象中。

首先,使服务对象返回temp_file:

class UserReportService
  def initialize(user)
    @user=user
  end

  def user_report_generate
    # Initialize DocxReplace with your template
    doc = DocxReplace::Doc.new("#{Rails.root}/lib/docx_templates/my_template.docx", "#{Rails.root}/tmp")

    # Replace some variables. $var$ convention is used here, but not required.
    doc.replace("$first_name$", @user.first_name)
    doc.replace("$last_name$", @user.last_name)
    doc.replace("$user_bio$", @user.bio)

    # Write the document back to a temporary file
    tmp_file = Tempfile.new('word_tempate', "#{Rails.root}/tmp")
    doc.commit(tmp_file.path)

    # Return the tmp_file
    tmp_file
  end
end

实例化您的服务对象,检索临时文件,并将其发送给用户:

def user_report
  respond_to do |format|
    format.docx do
      tmp_file = UserReportService.new(@user).user_report_generate
      send_file tmp_file.path, filename: "user_#{@user.id}_report.docx", disposition: 'attachment'
    end
  end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM