【问题标题】:respond_to and service objectsrespond_to 和服务对象
【发布时间】:2015-05-13 00:30:42
【问题描述】:

我目前正在使用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

然而,这使我的控制器膨胀,所以我试图将它放入一个服务对象中(继续上面的示例):

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

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

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'

我阅读了 respond_to,如果我正确理解了文档,这是控制器特有的方法(这可以解释问题)。我怎样才能解决这个问题?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 controller respond-to service-object


    【解决方案1】:

    respond_to 和 send_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
    

    【讨论】:

    • 您好,感谢您的回复!您的意思是将respond_to do |format| 和format.docx do 留在user_report_generate 方法中吗?正如您的答案所示,这些行同时出现在控制器和服务对象中;这是故意的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 2010-10-01
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    相关资源
    最近更新 更多