Commit 36ab9ceb by Mai Hoang Thai Ha

keep cv from apply to confirm

parent 5c77f39a
......@@ -2,7 +2,10 @@ class AppliesController < ApplicationController
before_action :load_job, only: %i[new confirm create]
def new
build_params = session[:apply_job] if use_apply_job_session?
if use_apply_job_session?
build_params = session[:apply_job]
@blob = ActiveStorage::Blob.find(session[:blob_id])
end
@apply = current_user.apply_jobs.build(build_params)
end
......@@ -11,6 +14,12 @@ class AppliesController < ApplicationController
@apply.job = @job
if @apply.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[:blob_id] = @blob.id
session[:apply_job] = @apply
else
render 'new'
......@@ -20,7 +29,7 @@ class AppliesController < ApplicationController
def create
@apply = current_user.apply_jobs.build(apply_params)
@apply.job = @job
@apply.cv.attach(params[:apply_job][:cv])
@apply.cv = ActiveStorage::Blob.find(session[:blob_id])
if @apply.save
UserMailer.apply_job(current_user, @job, @apply).deliver_now
......@@ -28,6 +37,7 @@ class AppliesController < ApplicationController
else
render 'new'
end
session[:blob_id] = nil
session[:apply_job] = nil
end
......@@ -41,7 +51,7 @@ class AppliesController < ApplicationController
end
def apply_params
params.require(:apply_job).permit(:user_name, :email, :cv)
params.require(:apply_job).permit(:user_name, :email, :cv, :job_id)
end
def current_user
......
......@@ -95,4 +95,19 @@ Validator.isEmail = function (selector) {
return regex.test(value) ? undefined : 'Email invalid';
}
}
}
\ No newline at end of file
}
Validator.fileTooLarge = function (selector) {
return {
selector: selector,
test: function (value) {
$(selector).on('change', function () {
const size = (this.files[0].size / 1024 / 1024).toFixed(2);
if (size > 5) {
alert('Maximum file size is 5MB. Please choose a smaller file.')
$(selector).val('');
}
})
}
}
}
javascript:
$("#apply_job_cv").bind("change", function() {
const size_in_megabytes = this.files[0].size/1024/1024;
if (size_in_megabytes > 5) {
alert("Maximum file size is 5MB. Please choose a smaller file.");
$("#apply_job_cv").val("");
}
});
\ No newline at end of file
......@@ -7,10 +7,11 @@
h1.my-5.text-center
| Confirmation
.col
= form_with(model: @apply, scope: :apply_job, url: done_path, local: true) do |f|
= form_with(model: @apply, scope: :apply_job, url: done_job_path, local: true) do |f|
.row.mb-5
= f.hidden_field :job_id, value: @job.id
= f.hidden_field :blob_id, value: @blob.id
.col-2
= f.label :name, 'Full name:', class: 'form-label label'
.col-10
......@@ -30,12 +31,10 @@
.col-2
= f.label :cv, 'Cv: ', class: 'form-label label'
.col-10
= f.file_field :cv, accept: ApplyJob::ACCEPT_CONTENT_TYPE
span.form-control
= url_for(@apply.cv)
= link_to 'Edit', :back, class: 'btn btn-secondary w-25 btn-height mr-5 my-5'
= link_to 'Edit', apply_job_path(job_id: @job.id), class: 'btn btn-secondary w-25 btn-height mr-5 my-5'
= f.submit 'Confirm', class: 'btn btn-primary w-25 btn-height mx-5 my-5'
= render 'validate_form'
- provide(:title, 'Apply job')
.container
= render 'ribbon'
.container
......@@ -7,7 +8,7 @@
p.fs-5.fw-bold.mb-4.text-center
= @job.title
.col
= form_with(model: @apply, url: confirm_path, local: true, id: 'apply-form') do |f|
= form_with(model: @apply, url: confirm_job_path, local: true, id: 'apply-form') do |f|
= render 'shared/error_messages', object: f.object
.row.mb-5.form-group
= f.hidden_field :job_id, value: @job.id
......@@ -28,20 +29,20 @@
.col-2
= f.label :cv, class: 'form-label label'
.col-10
= f.file_field :cv, accept: ApplyJob::ACCEPT_CONTENT_TYPE, class: 'form-control'
= f.file_field :cv, files: @blob, accept: ApplyJob::ACCEPT_CONTENT_TYPE, class: 'form-control'
span.form-msg
br
= f.submit 'Confirm', class: 'btn btn-primary w-25 my-4 btn-height',data: { disable_with: false }
= render 'validate_form'
javascript:
Validator({
form: '#apply-form',
errorSelector: '.form-msg',
rules: [
Validator.isRequired('#apply_job_user_name'),
Validator.isRequired('#apply_job_email'),
Validator.isRequired('#apply_job_cv'),
Validator.isEmail('#apply_job_email')
]
form: '#apply-form',
errorSelector: '.form-msg',
rules: [
Validator.isRequired('#apply_job_user_name'),
Validator.isRequired('#apply_job_email'),
Validator.isRequired('#apply_job_cv'),
Validator.fileTooLarge('#apply_job_cv'),
Validator.isEmail('#apply_job_email')
]
})
......@@ -22,7 +22,7 @@ ruby:
.job-apply.d-flex.align-items-center.justify-content-between
h2.align-items-start
= @job.title
= link_to 'Apply for this job', apply_path(job_id: @job.id), class: 'btn btn-primary'
= link_to 'Apply for this job', apply_job_path(job_id: @job.id), class: 'btn btn-primary'
p.text-secondary
= @job.company.name
.row.bg-light
......@@ -84,5 +84,5 @@ ruby:
li.text-secondary
= info
.job-apply.d-flex.align-items-center.justify-content-between
= link_to 'Apply for this job', apply_path(job_id: @job.id), class: 'btn btn-primary'
= link_to 'Apply for this job', apply_job_path(job_id: @job.id), class: 'btn btn-primary'
= link_to 'Favorite', '#', class: 'btn btn-primary'
......@@ -4,7 +4,9 @@
= "\n"
' Thank you for applied with VenJOB. Your applied job's information is as follow:
= "\n"
' Job title: #{@job.title}
' Job title: #{show_location(@job.cities)}
= "\n"
' Location: #{@job.title}
= "\n"
' Company: #{@job.company.name}
= "\n"
......
......@@ -7,7 +7,7 @@ Rails.application.routes.draw do
resources :jobs, only: %i[index show]
get '/apply', to: 'applies#new'
post '/confirm', to: 'applies#confirm'
post '/done', to: 'applies#create'
get '/apply', to: 'applies#new', as: :apply_job
post '/confirm', to: 'applies#confirm', as: :confirm_job
post '/done', to: 'applies#create', as: :done_job
end
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