store-file-cv

parent 8ca02714
Pipeline #1392 canceled with stages
in 0 seconds
class ApplyJobsController < ApplicationController class ApplyJobsController < ApplicationController
def new def new
job = Job.find_by(id: params[:job_id]) job = Job.find_by(id: params[:job_id])
return root_path unless job return root_path unless job
@apply_job = if session[:apply_job].present? if session[:apply_job].present?
ApplyJob.new(session[:apply_job]) @apply_job = ApplyJob.new(session[:apply_job])
@blob = ActiveStorage::Blob.find(session[:id])
@apply_job.assign_attributes(cv: @blob)
else else
ApplyJob.new @apply_job = ApplyJob.new
end end
@apply_job.job_id = job.id @apply_job.job = job
end end
def confirm def confirm
@apply_job = ApplyJob.new(apply_params) @apply_job = ApplyJob.new(apply_params)
@apply_job.user_id = User.find_by(id: 1).id @apply_job.user_id = User.find_by(id: 1).id
if @apply_job.valid? if @apply_job.valid?
@blob = ActiveStorage::Blob.create_and_upload!(
io: apply_params[:cv],
filename: apply_params[:cv].original_filename,
content_type: apply_params[:cv].content_type
)
session[:id] = @blob.id
session[:apply_job] = @apply_job session[:apply_job] = @apply_job
render :confirm
else else
render :new render :new
end end
...@@ -26,7 +34,8 @@ class ApplyJobsController < ApplicationController ...@@ -26,7 +34,8 @@ class ApplyJobsController < ApplicationController
@apply_job = ApplyJob.new(apply_params) @apply_job = ApplyJob.new(apply_params)
@apply_job.user_id = User.find_by(id: 1).id @apply_job.user_id = User.find_by(id: 1).id
@job = Job.latest_jobs.find(apply_params[:job_id]) @job = Job.latest_jobs.find(apply_params[:job_id])
if @apply_job.save @apply_job.cv = ActiveStorage::Blob.find(session[:id])
if @apply_job.save!
ApplyJobMailer.with(apply_job: @apply_job, job: @job).create_apply.deliver_now ApplyJobMailer.with(apply_job: @apply_job, job: @job).create_apply.deliver_now
flash.now[:success] = 'You have applied successfully' flash.now[:success] = 'You have applied successfully'
else else
...@@ -34,11 +43,12 @@ class ApplyJobsController < ApplicationController ...@@ -34,11 +43,12 @@ class ApplyJobsController < ApplicationController
redirect_to root_url redirect_to root_url
end end
session[:apply_job] = nil session[:apply_job] = nil
session[:id] = nil
end end
private private
def apply_params def apply_params
params.require(:apply_job).permit(:name, :email, :cv, :job_id) params.require(:apply_job).permit(:name, :email, :cv, :job_id, :id)
end end
end end
\ No newline at end of file
...@@ -15,9 +15,10 @@ h2.text-center ...@@ -15,9 +15,10 @@ h2.text-center
placeholder: "Type email...", class: 'form-control' placeholder: "Type email...", class: 'form-control'
= f.hidden_field :email = f.hidden_field :email
p.my-1 p.my-1
= f.label :cv, "CV" = f.label :cv, "Your CV"
p br
= f.file_field :cv, required: true, accept: "application/pdf", size: { less_than: 5.megabytes, message:"should be less than 5MB" } = f.hidden_field :id, value: @blob.id
= @apply_job.cv.filename
.span.p-3.text-center .span.p-3.text-center
= link_to "Back", apply_path(job_id: @apply_job.job_id), class: "btn btn-primary btn-space" = link_to "Back", apply_path(job_id: @apply_job.job_id), class: "btn btn-primary btn-space"
= f.submit "Done", class: "btn btn-primary btn-space" = f.submit "Done", class: "btn btn-primary btn-space"
\ No newline at end of file
...@@ -12,7 +12,9 @@ h2.text-center.form-title ...@@ -12,7 +12,9 @@ h2.text-center.form-title
= f.text_field :email, required: true, length: { maximum: 255 }, format: { with: ApplyJob::VALID_EMAIL_REGEX}, placeholder: "Type email...", class: 'form-control' = f.text_field :email, required: true, length: { maximum: 255 }, format: { with: ApplyJob::VALID_EMAIL_REGEX}, placeholder: "Type email...", class: 'form-control'
p.my-1 p.my-1
= f.label :cv, "CV" = f.label :cv, "CV"
p br
= @apply_job.cv.filename
br
= f.file_field :cv, required: true, accept: "application/pdf", size: { less_than: 5.megabytes, message:"should be less than 5MB" } = f.file_field :cv, required: true, accept: "application/pdf", size: { less_than: 5.megabytes, message:"should be less than 5MB" }
.span.p-3.text-center .span.p-3.text-center
= f.submit "Confirm", class: "btn btn-primary" = f.submit "Confirm", class: "btn btn-primary"
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment