0

我正在尝试将我的 PDF 附加到电子邮件中。但是,当我单击链接时,出现以下错误undefiined method 'user',我有以下设置:

控制器

 def index
    @hospital_bookings = HospitalBooking.scoped
    hospital_booking = @hospital_bookings
    @user = User.all

    if params[:format] == "pdf"
      @hospital_bookings = @hospital_bookings.where(:day => Date.today.beginning_of_month..Date.today.end_of_month)
    end

    respond_to do |format|
      format.html
      format.pdf do
        render :pdf => "#{Date.today.strftime("%B")} Overtime Report",
               :header => {:html => {:template => 'layouts/pdf.html.erb'}}
        OvertimeMailer.overtime_pdf(hospital_booking).deliver
      end
    end
  end

overtime_mailer.rb

class OvertimeMailer < ActionMailer::Base

  default :from => DEFAULT_FROM

  def overtime_pdf(hospital_booking)
    @hospital_bookings = hospital_booking
    attachments["Overtime Report.pdf"] = WickedPdf.new.pdf_from_string(
        render_to_string(:pdf => "Overtime Report",:template => 'layouts/pdf.html.erb'))
    mail(:to => hospital_booking.user.email, :subject => "Overtime Report")
  end
end

错误发生在以下行mail(:to => hospital_booking.user.email, :subject => "Overtime Report"

4

1 回答 1

0

hospital_booking在这段代码上

OvertimeMailer.overtime_pdf(hospital_booking).deliver

是一个ActiveRecord::Relation对象。您可能想打电话HospitalBooking.scoped.first或只是HospitalBooking.first在您的控制器中。

于 2013-03-06T00:49:30.783 回答