Commit 36ab9ceb by Mai Hoang Thai Ha

keep cv from apply to confirm

parent 5c77f39a
...@@ -2,7 +2,10 @@ class AppliesController < ApplicationController ...@@ -2,7 +2,10 @@ class AppliesController < ApplicationController
before_action :load_job, only: %i[new confirm create] before_action :load_job, only: %i[new confirm create]
def new 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) @apply = current_user.apply_jobs.build(build_params)
end end
...@@ -11,6 +14,12 @@ class AppliesController < ApplicationController ...@@ -11,6 +14,12 @@ class AppliesController < ApplicationController
@apply.job = @job @apply.job = @job
if @apply.valid? 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 session[:apply_job] = @apply
else else
render 'new' render 'new'
...@@ -20,7 +29,7 @@ class AppliesController < ApplicationController ...@@ -20,7 +29,7 @@ class AppliesController < ApplicationController
def create def create
@apply = current_user.apply_jobs.build(apply_params) @apply = current_user.apply_jobs.build(apply_params)
@apply.job = @job @apply.job = @job
@apply.cv.attach(params[:apply_job][:cv]) @apply.cv = ActiveStorage::Blob.find(session[:blob_id])
if @apply.save if @apply.save
UserMailer.apply_job(current_user, @job, @apply).deliver_now UserMailer.apply_job(current_user, @job, @apply).deliver_now
...@@ -28,6 +37,7 @@ class AppliesController < ApplicationController ...@@ -28,6 +37,7 @@ class AppliesController < ApplicationController
else else
render 'new' render 'new'
end end
session[:blob_id] = nil
session[:apply_job] = nil session[:apply_job] = nil
end end
...@@ -41,7 +51,7 @@ class AppliesController < ApplicationController ...@@ -41,7 +51,7 @@ class AppliesController < ApplicationController
end end
def apply_params def apply_params
params.require(:apply_job).permit(:user_name, :email, :cv) params.require(:apply_job).permit(:user_name, :email, :cv, :job_id)
end end
def current_user def current_user
......
...@@ -96,3 +96,18 @@ Validator.isEmail = function (selector) { ...@@ -96,3 +96,18 @@ Validator.isEmail = function (selector) {
} }
} }
} }
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 @@ ...@@ -7,10 +7,11 @@
h1.my-5.text-center h1.my-5.text-center
| Confirmation | Confirmation
.col .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 .row.mb-5
= f.hidden_field :job_id, value: @job.id = f.hidden_field :job_id, value: @job.id
= f.hidden_field :blob_id, value: @blob.id
.col-2 .col-2
= f.label :name, 'Full name:', class: 'form-label label' = f.label :name, 'Full name:', class: 'form-label label'
.col-10 .col-10
...@@ -30,12 +31,10 @@ ...@@ -30,12 +31,10 @@
.col-2 .col-2
= f.label :cv, 'Cv: ', class: 'form-label label' = f.label :cv, 'Cv: ', class: 'form-label label'
.col-10 .col-10
= f.file_field :cv, accept: ApplyJob::ACCEPT_CONTENT_TYPE
span.form-control span.form-control
= url_for(@apply.cv) = 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' = f.submit 'Confirm', class: 'btn btn-primary w-25 btn-height mx-5 my-5'
= render 'validate_form'
- provide(:title, 'Apply job') - provide(:title, 'Apply job')
.container .container
= render 'ribbon' = render 'ribbon'
.container .container
...@@ -7,7 +8,7 @@ ...@@ -7,7 +8,7 @@
p.fs-5.fw-bold.mb-4.text-center p.fs-5.fw-bold.mb-4.text-center
= @job.title = @job.title
.col .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 = render 'shared/error_messages', object: f.object
.row.mb-5.form-group .row.mb-5.form-group
= f.hidden_field :job_id, value: @job.id = f.hidden_field :job_id, value: @job.id
...@@ -28,12 +29,11 @@ ...@@ -28,12 +29,11 @@
.col-2 .col-2
= f.label :cv, class: 'form-label label' = f.label :cv, class: 'form-label label'
.col-10 .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 span.form-msg
br br
= f.submit 'Confirm', class: 'btn btn-primary w-25 my-4 btn-height',data: { disable_with: false } = f.submit 'Confirm', class: 'btn btn-primary w-25 my-4 btn-height',data: { disable_with: false }
= render 'validate_form'
javascript: javascript:
Validator({ Validator({
form: '#apply-form', form: '#apply-form',
...@@ -42,6 +42,7 @@ javascript: ...@@ -42,6 +42,7 @@ javascript:
Validator.isRequired('#apply_job_user_name'), Validator.isRequired('#apply_job_user_name'),
Validator.isRequired('#apply_job_email'), Validator.isRequired('#apply_job_email'),
Validator.isRequired('#apply_job_cv'), Validator.isRequired('#apply_job_cv'),
Validator.fileTooLarge('#apply_job_cv'),
Validator.isEmail('#apply_job_email') Validator.isEmail('#apply_job_email')
] ]
}) })
...@@ -22,7 +22,7 @@ ruby: ...@@ -22,7 +22,7 @@ ruby:
.job-apply.d-flex.align-items-center.justify-content-between .job-apply.d-flex.align-items-center.justify-content-between
h2.align-items-start h2.align-items-start
= @job.title = @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 p.text-secondary
= @job.company.name = @job.company.name
.row.bg-light .row.bg-light
...@@ -84,5 +84,5 @@ ruby: ...@@ -84,5 +84,5 @@ ruby:
li.text-secondary li.text-secondary
= info = info
.job-apply.d-flex.align-items-center.justify-content-between .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' = link_to 'Favorite', '#', class: 'btn btn-primary'
...@@ -4,7 +4,9 @@ ...@@ -4,7 +4,9 @@
= "\n" = "\n"
' Thank you for applied with VenJOB. Your applied job's information is as follow: ' Thank you for applied with VenJOB. Your applied job's information is as follow:
= "\n" = "\n"
' Job title: #{@job.title} ' Job title: #{show_location(@job.cities)}
= "\n"
' Location: #{@job.title}
= "\n" = "\n"
' Company: #{@job.company.name} ' Company: #{@job.company.name}
= "\n" = "\n"
......
...@@ -7,7 +7,7 @@ Rails.application.routes.draw do ...@@ -7,7 +7,7 @@ Rails.application.routes.draw do
resources :jobs, only: %i[index show] resources :jobs, only: %i[index show]
get '/apply', to: 'applies#new' get '/apply', to: 'applies#new', as: :apply_job
post '/confirm', to: 'applies#confirm' post '/confirm', to: 'applies#confirm', as: :confirm_job
post '/done', to: 'applies#create' post '/done', to: 'applies#create', as: :done_job
end 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