2017-08-31 85 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 
相關問題