Commit eefc3fcf by Tô Ngọc Ánh

create ID12; add create, destroy favorite function

parent 803abcc4
Pipeline #961 failed with stages
in 0 seconds
class AppliedJobsController < ApplicationController
before_action :authenticate_user!
def index
@applied_jobs = Job.applied_jobs_of(current_user.id).includes(:locations).page(params[:page])
end
def new
job = Job.find_by(id: params[:job_id])
return redirect_to root_path if job.nil?
@applied_job = job.applied_jobs.new(full_name: current_user.full_name,
email: current_user.email)
end
def confirm
@applied_job = current_user.applied_jobs.new(applied_job_params)
@applied_job.curriculum_vitae = current_user.curriculum_vitae if params[:applied_job][:curriculum_vitae].nil?
if @applied_job.invalid?
flash[:error] = @applied_job.errors.full_messages
redirect_to new_applied_job_path(job_id: @applied_job.job_id)
end
end
def finish
@applied_job = current_user.applied_jobs.new(applied_job_params)
@applied_job.curriculum_vitae.retrieve_from_cache!(params[:applied_job][:curriculum_vitae])
if @applied_job.save
AppliedJobMailer.with(applied_job: @applied_job).success_email.deliver_later
else
flash[:error] = @applied_job.errors.full_messages
redirect_to new_applied_job_path(job_id: @applied_job.job_id)
end
end
private
def applied_job_params
params.require(:applied_job).permit(:job_id, :full_name, :email, :curriculum_vitae)
end
end
class FavoritesController < ApplicationController
before_action :authenticate_user!
def index
end
def create
@job = Job.find_by(id: params[:job_id])
@favorite = @job.favorites.new(user_id: current_user.id)
respond_to :js if @favorite.save
end
def destroy
@job = Job.find_by(id: params[:job_id])
favorite = Favorite.find_by(id: params[:id])
respond_to :js if favorite.destroy
end
end
......@@ -5,6 +5,7 @@ class JobsController < ApplicationController
object = params[:model].classify.constantize.find_by_slug(params[:slug])
@keyword = object.try(:name) || object.try(:city)
@jobs = object.jobs.all.includes(:company, :locations, :industries).page(params[:page])
# @is_favorited = user_signed_in? ? current_user.favorites.find_by(job_id: )
end
def show
......
class Job < ApplicationRecord
scope :applied_jobs_of, ->(user_id) do
joins(:applied_jobs)
.where("applied_jobs.user_id = #{user_id}")
.select('jobs.*, applied_jobs.created_at as applied_at')
end
NUMBER_LATEST_JOB = 6
WORDS_SHORT_DESCRIPTION = 250
......
<div class= "card my-2">
<div class="row">
<div class = "col-lg-10">
<div class='card-body'>
<%= link_to job.title, job_path(job), class: 'card-title font-weight-bold text-decoration-none' %>
<p class='mb-0'>
<strong>Work place:</strong>
<% job.locations.each do |location| %>
<%= location.city %>
<% end %>
</p>
<p><strong>Salary: </strong><%= job.salary %></p>
<p class='card-text'><%= strip_tags(job.description).truncate(Job::WORDS_SHORT_DESCRIPTION) %></p>
</div>
</div>
<div class = "col-lg-2 py-4">
<p><strong>Applied at: </strong></p>
<span><%= job.applied_at.localtime.strftime('%d/%m/%y - %H:%M') %></span>
</div>
</div>
</div>
<div class="my-4">
<h3 class='text-center'>Applied Jobs</h3>
<hr class="divider">
<div class='content'>
<%= paginate @applied_jobs %>
<%= render partial: 'applied_jobs/applied_job', collection: @applied_jobs, as: :job %>
<%= paginate @applied_jobs %>
</div>
</div>
<%= link_to 'Favorite', job_favorites_path(job),
method: :post, class: 'btn btn-outline-danger btn-lg', remote: true,
disable_with: '<i class="fa fa-spinner fa-spin"></i>'.html_safe %>
\ No newline at end of file
<%= link_to 'Unfavorite', job_favorite_path(job_id: job_id, id: favorite_id),
method: :delete, class: 'btn btn-outline-secondary btn-lg', remote: true,
disable_with: '<i class="fa fa-spinner fa-spin"></i>'.html_safe %>
\ No newline at end of file
$('#favorite-<%= @job.id %>')
.html("<%= j render 'favorites/link_destroy', job_id: @job.id, favorite_id: @favorite.id %>")
\ No newline at end of file
$('#favorite-<%= @job.id %>')
.html("<%= j render 'favorites/link_create', job: @job %>")
\ No newline at end of file
......@@ -11,7 +11,7 @@
<p><strong>Salary: </strong><%= job.salary %></p>
<p class='card-text'><%= strip_tags(job.description).truncate(Job::WORDS_SHORT_DESCRIPTION) %></p>
</div>
<div class='btn-favorite p-2'>
<%= link_to 'Favorite', '#', class: 'btn btn-outline-danger btn-lg' %>
<div id="favorite-<%= job.id %>" class='p-2'>
<%= render 'favorites/link_create', job: job %>
</div>
</div>
......@@ -10,7 +10,7 @@
<ul class='navbar-nav ml-auto'>
<% if user_signed_in? %>
<li class="nav-item">
<%= link_to '#', class: 'nav-link text-white', title: 'Favorite' do %>
<%= link_to my_favorites_path, class: 'nav-link text-white', title: 'Favorite' do %>
<i class="fas fa-heart"></i>
<% end %>
</li>
......
......@@ -27,7 +27,7 @@
<%= link_to 'Update', edit_user_registration_path, class: 'btn btn-outline-primary btn-lg' %>
</div>
<div class="col text-left">
<%= link_to 'My Jobs', '#', class: 'btn btn-outline-primary btn-lg' %>
<%= link_to 'My Jobs', my_applied_jobs_path, class: 'btn btn-outline-primary btn-lg' %>
</div>
</div>
<% end %>
......
......@@ -6,6 +6,11 @@ Rails.application.routes.draw do
get 'detail/:id', to: 'jobs#show', as: :job
get 'jobs/:model/:slug', to: 'jobs#index', as: :jobs
get 'my', to: 'users#my_page', as: :my_page
get 'my/jobs', to: 'applied_jobs#index', as: :my_applied_jobs
get 'my/favorite', to: 'favorites#index', as: :my_favorites
post 'jobs/:job_id/favorites', to: 'favorites#create', as: :job_favorites
delete 'jobs/:job_id/favorites/:id', to: 'favorites#destroy', as: :job_favorite
resources :users, only: :show
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
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