2017-08-31 132 views
1

我有一种形式,当潜在的雇用有兴趣为公司工作时,他/她填写并附上简历。提交材料将发送给附有附件的公司代表。电子邮件正在通过,但附件不是文件,我不知道如何正确配置它。在提交电子邮件附件只是说“文件”。通过电子邮件发送回形针文件作为附件 - Rails

career_mailer.rb

class CareerMailer < ApplicationMailer 

    default from: "[email protected]" 



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

career.rb(模型)

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+\-].?)[email protected][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 

UPDATE

我想在我的职业生涯邮件如下:

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

我收到以下错误:

error

更新2

我仍然在这一点,但我的工作根据我读过的所有内容进行思考我需要在将回形针文件保存到模型之前拉动回形针文件,以便我将弄清楚如何这样做,以便我可以将上传的简历作为附件。

回答

0

经过数小时的反复试验,我终于弄明白了,因为它只有1行。基本上所有我需要做的就是下面添加到我的career_mailer.rb:

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

document_file_name实际上是在我的表中的列,其中回形针保存文档的名称的名称。如果您使用回形针作为文件,图像等,这可能会改变。我选择使用word文档。

这是为我工作的最终产品:

class CareerMailer < ApplicationMailer 

    default from: "[email protected]" 



    def career_inquiry(career) 
    @career = career 

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



    mail(to: "[email protected]", subject: "This is just a test from Jay") 
    end 
end 
相关问题