Commit 9ebefcc0 by nnnghia98

check user_jobs exist?

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