如何在调用render_to_string呈现模板时使Rails辅助方法可用

 大师兄v断水流 发布于 2023-02-06 10:50

我有一个ActiveMailer类,在里面我使用render_to_string方法发送附带PDF模板的电子邮件,如下所示:

 def send_sales_order(email_service)
    @email_service = email_service
    @sales_order = SalesOrder.find_by_cid(email_service.order[:id])
    mail(:subject => I18n.t("custom_mailer.sales_order.subject", company_name: "test"), :to => @email_service.recipients) do |format|
      format.html
      format.pdf do
        attachments['purchase_order.pdf'] = WickedPdf.new.pdf_from_string(
          render_to_string('sales_orders/show.pdf.erb', locals: {current_company: @sales_order.company})
        )
      end
    end
  end

在show.pdf.erb模板中,我调用了我在其他地方定义的辅助方法,例如在ApplicationController中定义的current_company方法,如下所示:

    class ApplicationController < ActionController::Base
      helper_method :current_company, :current_role

      def current_company
        return if current_user.nil?
        if session[:current_company_id].blank?
          session[:current_company_id] = current_user.companies.first.id.to_s
        end
        @current_company ||= Company.find(session[:current_company_id])
      end

但是当我使用render_to_string方法渲染模板时,这些辅助方法不可用,有没有办法解决这个问题?

谢谢

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有