Commit cbcd57ee by Mai Hoang Thai Ha

fixed button

parent 6ce36e02
......@@ -2,22 +2,18 @@ class FavoriteJobsController < ApplicationController
before_action :logged_in_user
before_action :load_job, only: %i[create destroy]
def index
@apply_jobs = Job.apply_jobs_of(current_user.id).sort_by_date(page: params[:page], per_page: Job::JOB_PER_PAGE)
end
def create
@favorite_job = current_user.favorite_jobs.build(job_id: @job.id)
@favorite_job.save
respond_to do |format|
format.html { redirect_back(fallback_location: root_path) }
format.js
end
respond_to :js if @favorite_job.save
end
def destroy
@favorite_job = current_user.favorite_jobs.find_by(job_id: @job.id)
@favorite_job.destroy
respond_to do |format|
format.html { redirect_back(fallback_location: root_path) }
format.js
end
respond_to :js if @favorite_job.destroy
end
private
......
......@@ -2,7 +2,8 @@ class HistoryJobsController < ApplicationController
before_action :logged_in_user
def index
job_id_list = current_user.history_jobs.map(&:job_id)
@jobs = Job.includes(:cities, :cities_jobs, :company).references(:cities).find(job_id_list)
# job_id_list = current_user.history_jobs.map(&:job_id)
# @jobs = Job.includes(:cities, :cities_jobs, :company).references(:cities).find(job_id_list)
@history_jobs = Job.history_jobs_of(current_user.id)
end
end
......@@ -5,7 +5,7 @@ class JobsController < ApplicationController
if job_params.present?
search
else
@jobs = Job.sort_by_date(page: params[:page], per_page: Job::JOB_PER_PAGE)
@jobs = Job.joins(:favorite_jobs).sort_by_date(page: params[:page], per_page: Job::JOB_PER_PAGE)
end
end
......
class Job < ApplicationRecord
scope :history_jobs_of, ->(user_id) do
joins(:history_jobs)
.where("history_jobs.user_id = #{user_id}")
.includes(:cities, :cities_jobs, :company).references(:cities)
end
LATEST_JOBS_LIMIT = 5
JOB_PER_PAGE = 20
......@@ -7,7 +13,7 @@ class Job < ApplicationRecord
belongs_to :company
has_many :apply_jobs, dependent: :destroy
has_many :favorite_jobs, dependent: :destroy
has_many :history_job, dependent: :destroy
has_many :history_jobs, dependent: :destroy
def self.sort_by_date(page: 1, per_page: 1)
includes(:cities, :cities_jobs, :company).order(created_at: :desc).page(page).per(per_page).references(:cities)
......
$("#favorite-<%= @job.id").html("<%= escape_javascript(render "shared/unfavorite", job_id: @job.id) %>"); %>
$("#favorite-<%= @job.id %>").html("<%= escape_javascript(render('shared/unfavorite', job_id: @job.id)) %>");
$("#favorite-<%= @job.id %>").html("<%= escape_javascript(render "shared/favorite", job_id: @job.id) %>");
$("#unfavorite-<%= @job.id %>").html("<%= escape_javascript(render('shared/favorite', job_id: @job.id)) %>");
- provide(:title, 'Job list page')
/ search box
= render 'shared/search'
.container
h1.mt-5
= @name
hr.my-4
.no-padding.d-flex.align-items-center.flex-column
.page-info.p-2
= page_entries_info @apply_jobs
.page-info.p-2
= paginate @apply_jobs
.container
.container
- @apply_jobs.each do |job|
/ job
.job-item
.job-head.d-flex.align-items-center.justify-content-between
= link_to job.title, job, class: 'job-title fs-3 text-decoration-none text-reset'
- if user_signed_in? && current_user.favorite?(job)
= render 'shared/unfavorite', job_id: job.id
- else
= render 'shared/favorite', job_id: job.id
.job-caption
= link_to job.company.name, '#', class: 'job-company text-decoration-none text-secondary'
p.job-salary.text-success
| Salary: #{job.salary}
ul.list-unstyled
= show_location(job.cities)
.job-desc
= truncate(simple_format(job.description), escape: false, length: 250)
hr.my-4
.no-padding.d-flex.align-items-center.flex-column
.page-info.p-2
= page_entries_info @apply_jobs
.page-info.p-2
= paginate @apply_jobs
\ No newline at end of file
.container
.my-5
- @jobs.each do |job|
- @history_jobs.each do |job|
.job-item
.job-head.d-flex.align-items-center.justify-content-between
= link_to job.title, job, class: 'job-title fs-3 text-decoration-none text-reset'
- if user_signed_in? && current_user.favorite?(job)
= render 'shared/unfavorite', job_id: job.id
- else
= render 'shared/favorite', job_id: job.id
.job-caption
= link_to job.company.name, '#', class: 'job-company text-decoration-none text-secondary'
p.job-salary.text-success
......
= form_with(model: @favorite_job, url: favorite_jobs_path(job_id: job_id), id: "favorite-#{job_id}", remote: true) do |f|
= f.submit 'Favorite', class: 'btn btn-primary btn-height'
/ = form_with(model: @favorite_job, url: favorite_jobs_path(job_id: job_id, format: :js), id: "favorite-#{job_id}", remote: true) do |f|
/ = f.submit 'Favorite', class: 'btn btn-primary btn-height'
div id="favorite-#{job_id}"
= link_to favorite_jobs_path(job_id: job_id, format: :js),
method: :post,
remote: true,
class: 'btn btn-primary'
| Favorite
\ No newline at end of file
= form_with(model: @favorite_job, url: favorite_job_path(current_user, job_id: job_id), id: "unfavorite-#{job_id}", method: :delete, remote: true) do |f|
= f.submit 'Un favorite', class: 'btn btn-secondary btn-height'
/ = form_with(model: @favorite_job, url: favorite_job_path(current_user, job_id: job_id, format: :js), id: "unfavorite-#{job_id}", method: :delete, remote: true) do |f|
/ = f.submit 'Un favorite', class: 'btn btn-secondary btn-height'
div id="unfavorite-#{job_id}"
= link_to favorite_job_path(current_user, job_id: job_id, format: :js),
method: :delete,
remote: true,
class: 'btn btn-secondary'
| UnFavorite
\ No newline at end of file
......@@ -16,7 +16,7 @@ Rails.application.routes.draw do
resources :cities, only: %i[index]
resources :industries, only: %i[index]
resources :favorite_jobs, only: %i[create destroy]
resources :favorite_jobs, only: %i[index create destroy]
resources :jobs, only: %i[index show]
get '/jobs/:model/:slug', to: 'jobs#index', as: :job_list
......
This diff is collapsed. Click to expand it.
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