Commit 9ebefcc0 by nnnghia98

check user_jobs exist?

parent e0aa1263
class FavoritesController < ApplicationController
before_action :authenticate_user!, only: [:create, :destroy]
before_action :find_favorited_job, only: [:create, :destroy]
def create
redirect_to new_user_session_path unless user_signed_in?
redirect_to jobs_path if @job.id.blank?
job_id = @job.id
@all_favorite_job.find_by(job_id: job_id) ||
@all_favorite_job.create!(user_id: current_user.id, job_id: job_id, favorited_at: Time.current)
redirect_to job_path(job_id)
redirect_to jobs_path if params[:id].blank?
job_id = params[:id]
if get_user_job
@favorite_jobs = get_user_job.where.not(favorited_at: nil) ||
get_user_job.update(favorited_at: Time.current)
else
@favorite_jobs = UserJob.create!(user_id: current_user.id, job_id: job_id, favorited_at: Time.current)
end
end
def destroy
redirect_to new_user_session_path unless user_signed_in?
redirect_to jobs_path if @job.id.blank?
job_id = params[:id]
job_id = @job.id
@all_favorite_job.find_by(job_id: job_id).destroy
@favorited_jobs = get_user_job.where.not(favorited_at: nil)
@favorited_jobs = get_user_job.update(favorited_at: nil)
redirect_to job_path(job_id)
end
private
def find_favorited_job
@all_favorite_job = current_user.user_jobs.where.not(favorited_at: nil)
def get_user_job
UserJob.find_by(user_id: current_user.id, job_id: params[:id])
end
end
......@@ -68,6 +68,7 @@ class JobsController < ApplicationController
elsif params[:industry_id]
redirect_to jobs_path unless Industry.find_by(id: params[:industry_id])
end
end
def find_applied_jobs
@all_applied_jobs = current_user.user_jobs.where.not(applied_at: nil)
......
$("#favorite_form_<%= @job.id %>").html("<%= escape_javascript(render "shared/unfavorite_btn", job: @job %>");
$("#favorite_form").html("<%= escape_javascript(render "shared/unfavorite_btn", job: @job %>");
$("#favorite_form_<%= @job.id %>").html("<%= escape_javascript(render "shared/favorite_btn", job: @job) %>");
$("#favorite_form").html("<%= escape_javascript(render "shared/favorite_btn", job: @job) %>");
......@@ -17,8 +17,7 @@
<dd><%= job["city"] %></dd>
</dl>
</div>
<div id="favorite_form_">
<button type="button" class="btn btn-outline-secondary">Favorite</button>
<div id="favorite_form">
<% if current_user.favorites.exists?(job_id: job.id) %>
<%= render "shared/unfavorite_btn", job_id: job.id %>
<% else %>
......
<% provide(:title, "Favorite") %>
<h1>VeNJOB Favorite</h1>
......@@ -22,6 +22,7 @@ default: &default
secure_auth: false
timeout: 5000
socket: /var/run/mysqld/mysqld.sock
reconnect: true
development:
<<: *default
......
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