簡體   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