【问题标题】:Rails render_to_string "undefined method" for custom helper自定义助手的 Rails render_to_string“未定义方法”
【发布时间】:2020-11-25 04:54:30
【问题描述】:

导轨 6...

当我将浏览器指向视图时,我有一个自定义助手可以正常工作。但我在其他地方使用相同的视图生成附加到电子邮件的 PDF(使用 render_to_string)。在这种情况下,我会收到 undefined method 错误。

这是 app/views/submissions/show.slim(这是一个 React SPA):

= javascript_tag do
  | var rawForm =  #{raw(@submission.frm.to_json)}

= pdf_stylesheet_pack_tag 'shower'
= pdf_javascript_pack_tag 'shower'

这里是 app/helpers/pdf_helper.rb

module PdfHelper
  def pdf_stylesheet_pack_tag(source)
    if running_in_development?
      options = { media: "all" }
      wds = Webpacker.dev_server
      options[:host] = "#{wds.host}:#{wds.port}" unless show_as_html?
      stylesheet_pack_tag(source, options)
    else
      wicked_pdf_stylesheet_pack_tag(source)
    end
  end

  def pdf_javascript_pack_tag(source)
    if running_in_development?
      options = {}
      wds = Webpacker.dev_server
      options[:host] = "#{wds.host}:#{wds.port}" unless show_as_html?
      javascript_pack_tag(source, options)
    else
      wicked_pdf_javascript_pack_tag(source)
    end
  end

  def show_as_html?
    params[:debug].present?
  end

end

(此助手解决了 Wicked PDF gem 的问题。)

这里是 app/mailers/notifications_mailer.rb

class NotificationsMailer < ApplicationMailer
  def new_submission(submission, email)
    @submission = submission
    @frm = submission.frm
    attachments["submission-#{submission.id}.pdf"] = WickedPdf.new.pdf_from_string(
      render_to_string(
        'submissions/show',
        layout: 'pdf',
        locals: {:@submission => submission}
      )
    )
    mail to: email
  end
end

当邮件程序尝试发送电子邮件时,我在日志中看到此错误:

[ActiveJob] [ActionMailer::MailDeliveryJob] [f8ce8ecc-2ae5-447d-85f0-bd46bf3c8b20]   Rendered submissions/show.slim within layouts/pdf (Duration: 39.5ms | Allocations: 12324)
[ActiveJob] [ActionMailer::MailDeliveryJob] [f8ce8ecc-2ae5-447d-85f0-bd46bf3c8b20] NotificationsMailer#new_submission: processed outbound mail in 444.2ms
[ActiveJob] [ActionMailer::MailDeliveryJob] [f8ce8ecc-2ae5-447d-85f0-bd46bf3c8b20] Error performing ActionMailer::MailDeliveryJob (Job ID: f8ce8ecc-2ae5-447d-85f0-bd46bf3c8b20) from Async(mailers) in 444.67ms: ActionView::Template::Error (undefined method `pdf_javascript_pack_tag' for #<#<Class:0x00007f8c728defb8>:0x00007f8c728dcc68>):
/Users/emerson/Code/consent/app/views/submissions/show.slim:6:in `_app_views_submissions_show_slim__295365913496948297_70120585776840'

我错过了什么?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-6


    【解决方案1】:

    render_to_stringActionController 类的方法。试试下面的方法来使用控制器之外的方法render_to_string(它继承自ActionController)。

    ApplicationController.new.render_to_string('submissions/show',
            layout: 'pdf',
            locals: {:@submission => submission})
    

    【讨论】:

    • 新错误:``` [ActiveJob] [ActionMailer::MailDeliveryJob] [74cfaa5c-d6e7-4474-9ade-1f0b0f3bdb3b] 执行 ActionMailer::MailDeliveryJob 时出错(作业 ID:74cfaa5c-d6e7-4474- 9ade-1f0b0f3bdb3b) 在 1024.51 毫秒内来自异步(邮件程序):ActionView::Template::Error(未定义方法parameters' for nil:NilClass): /Users/emerson/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/actionpack-6.0.2.2/lib/action_controller/metal/strong_parameters.rb:1095:in params'/Users/emerson/.rbenv/versions/2.6.3/lib/ruby/gems/2.6。 0/gems/actionview-6.0.2.2/lib/action_view/helpers/controller_helper.rb:16:in `params' ```
    • 你能分享邮件的视图文件吗?
    猜你喜欢
    • 2014-03-28
    • 2018-08-10
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 2013-06-11
    • 2014-02-07
    • 1970-01-01
    • 2015-08-11
    相关资源
    最近更新 更多