Reputation: 796
I have a form that when a potential hire is interested in working for the company he/she fills it out and attaches a resume. The submission will go to a company representative with the attachment. The email is going through but the attachment is not the document and I cannot figure out how to configure it properly. In the submission email the attachment just says "document".
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 (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
UPDATE
I am trying the following in my career mailer:
attachments[career.document.attach_file_name] = File.read(career.document.attach.path)
I am getting the following error:
UPDATE 2
I am still working on this but I think based on everything I've read I need to pull the paperclip file before it saves to the model so I am going to figure out how to do so so I can send the uploaded resume as an attachment.
Upvotes: 2
Views: 1132
Reputation: 796
After hours of trial and error I finally figured it out and it sucks cause it was only 1 line. Basically all I had to do was add the following to my career_mailer.rb:
attachments[@career.document_file_name] = File.read(@career.document.path )
The document_file_name
is actually the name of the column in my table where paperclip saves the name of the document. This could change if you used paperclip for file, image, et cetera. I chose to use the word document.
This is the final product that worked for me:
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
Upvotes: 3