通过电子邮件发送回形针文件作为附件 - Rails

时间:2017-08-31 22:36:43

标签: ruby-on-rails paperclip ruby-on-rails-5

我有一份表格,当潜在的雇员有兴趣为公司工作时,他/她填写并附上简历。提交将通过附件发送给公司代表。电子邮件正在通过,但附件不是文档,我无法弄清楚如何正确配置它。在提交电子邮件中,附件只是说“文档”。

career_mailer.rb

class CareerMailer < ApplicationMailer

  default from: "career@conciergenursingdirect.com"



  def career_inquiry(career)
    @career = career
     attachments['attachment.extension'] = document
    mail(to: "michele@conciergenursingdirect.com", subject: "This is just a test from Jay")
  end
end

career.rb(model)

class Career < ApplicationRecord

    has_attached_file :document

    validates_attachment_size :document, :less_than => 25.megabytes    
    validates_attachment_presence :document
     validates_attachment_content_type :document, :content_type => ["application/pdf","application/vnd.ms-excel",     
                                                                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                                                                    "application/msword", 
                                                                    "application/vnd.openxmlformats-officedocument.wordprocessingml.document", 
                                                                    "text/plain"]

    email_regex = /\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i

    validates :name, :presence => true,
              :length          => { :maximum => 50 }
    validates :subject, :presence => true,
              :length          => { :maximum => 50 }
    validates :phone, :presence => true,
    :length          => { :maximum => 50 }
    validates :email, :presence => true,
              :format          => {:with => email_regex }
    validates :message, :presence => true,
              :length          => { :maximum => 5000 }

end

careers_controller.rb

class CareersController < ApplicationController
  def new
  @career = Career.new

  end
  def show
    @career = Career.find(params[:id])
  end 

  def create
    # fail
    @career = Career.create(career_params)
    if @career.save
      CareerMailer.career_inquiry(@career).deliver
      redirect_back(fallback_location: root_path)
    else
      flash[:error] = @career.errors.full_messages
      redirect_back(fallback_location: root_path)
    end


  end
  private
  def career_params
    params.require(:career).permit(:name, :phone, :subject, :email, :message, :document)
  end
end

更新

我正在我的职业邮件中尝试以下内容:

attachments[career.document.attach_file_name] = File.read(career.document.attach.path)

我收到以下错误:

error

更新2

我还在努力,但我认为根据我读过的所有内容,我需要在保存到模型之前拉回纸夹文件,这样我就会弄清楚如何这样做,这样我就可以发送上传的简历了作为附件。

1 个答案:

答案 0 :(得分:2)

经过几个小时的反复试验后,我终于弄明白了,它很糟糕,因为它只有1行。基本上我所要做的就是将以下内容添加到我的career_mailer.rb中:

attachments[@career.document_file_name] = File.read(@career.document.path )

document_file_name实际上是我的表中列的名称,其中paperclip保存文档的名称。如果您将paperclip用于文件,图像等,则可能会发生这种情况。我选择使用word文档。

这是对我有用的最终产品:

class CareerMailer < ApplicationMailer

  default from: "career@conciergenursingdirect.com"



  def career_inquiry(career)
    @career = career

    attachments['resume'] = File.read( @career.document.path ) 



    mail(to: "michele@conciergenursingdirect.com", subject: "This is just a test from Jay")
  end
end