Commit e105f69b by Ngô Trung Hưng

fix code favorite

parent e17efe61
Pipeline #981 canceled with stages
in 0 seconds
...@@ -4,7 +4,7 @@ class FavoriteController < ApplicationController ...@@ -4,7 +4,7 @@ class FavoriteController < ApplicationController
before_action :authenticate_user! before_action :authenticate_user!
def index def index
@favorites = current_user.favorites.map(&:job) @favorites = current_user.favorites.order(created_at: :desc).map(&:job)
end end
def create def create
...@@ -13,13 +13,13 @@ class FavoriteController < ApplicationController ...@@ -13,13 +13,13 @@ class FavoriteController < ApplicationController
@favorite = current_user.favorites.new(job_id: params[:job_id]) @favorite = current_user.favorites.new(job_id: params[:job_id])
if @favorite.invalid? if @favorite.invalid?
helpers.render_errors(@favorite) helpers.render_errors(@favorite)
redirect_to favorite_index_path return redirect_to favorite_index_path
end end
respond_to :js if @favorite.save respond_to :js if @favorite.save
end end
def destroy def destroy
@favorite = current_user.favorites.find_by(id: params[:favorite_id]) @favorite = current_user.favorites.find_by(id: params[:id])
return redirect_to favorite_index_path if @favorite.blank? return redirect_to favorite_index_path if @favorite.blank?
respond_to :js if @favorite.destroy respond_to :js if @favorite.destroy
......
...@@ -3,6 +3,12 @@ ...@@ -3,6 +3,12 @@
# Job controller # Job controller
class JobController < ApplicationController class JobController < ApplicationController
before_action :load_data_dropdown, only: :index before_action :load_data_dropdown, only: :index
# after_action :add_job_to_history, only: :detail
def add_job_to_history
current_user.histories.create(session[:params_job_id]) if user_signed_in?
session.delete(:params_job_id)
end
def index def index
model = params[:model].classify.constantize model = params[:model].classify.constantize
...@@ -11,6 +17,7 @@ class JobController < ApplicationController ...@@ -11,6 +17,7 @@ class JobController < ApplicationController
end end
def detail def detail
session[:params_job_id] = params[:id]
@job = Job.find(params[:id]).decorate @job = Job.find(params[:id]).decorate
cities = @job.cities.first cities = @job.cities.first
industries = @job.industries.first industries = @job.industries.first
......
...@@ -11,12 +11,18 @@ module ApplicationHelper ...@@ -11,12 +11,18 @@ module ApplicationHelper
flash_messages = [] flash_messages = []
flash.each do |type, message| flash.each do |type, message|
next if message.blank? next if message.blank?
message.delete!("'") if message.include?("'")
type = 'success' if type == 'notice' type = 'success' if type == 'notice'
type = 'error' if type == 'alert' type = 'error' if type == 'alert'
type = 'warning' if type == 'alert'
text = "<script>toastr.#{type}('#{message}');</script>" text = "<script>toastr.#{type}('#{message}');</script>"
flash_messages << text.html_safe if message flash_messages << text.html_safe if message
end end
flash_messages.join("\n").html_safe flash_messages.join("\n").html_safe
end end
def render_errors(obj)
errors = []
obj.errors.full_messages.each { |mess| errors << "#{mess}<br>" }
flash[:error] = errors.join('<br>').html_safe
end
end end
# frozen_string_literal: true # frozen_string_literal: true
module ApplyJobHelper module ApplyJobHelper
def render_errors(obj)
errors = []
obj.errors.full_messages.each { |mess| errors << "#{mess}<br>" }
flash[:error] = errors.join('<br>').html_safe
end
end end
module FavoriteHelper module FavoriteHelper
def render_errors(obj)
obj.errors.full_messages.each { |mess| flash[:warning] = mess }
end
end end
...@@ -39,9 +39,9 @@ ...@@ -39,9 +39,9 @@
<div class="col-sm-2 col-md-3 d-none d-sm-block col-lg-2"> <div class="col-sm-2 col-md-3 d-none d-sm-block col-lg-2">
<div class="link_favorite_top block_link_favorite"> <div class="link_favorite_top block_link_favorite">
<% if user_signed_in? %> <% if user_signed_in? %>
<% @favorite_id = current_user.favorites.find_by(job_id: data.id) %> <% favorite_id = current_user.favorites.find_by(job_id: data.id) %>
<% if favorite_id.present? %> <% if favorite_id.present? %>
<%= render 'job/link_destroy', favorite_id: @favorite_id %> <%= render 'job/link_destroy', favorite_id: favorite_id %>
<% else %> <% else %>
<%= render 'job/link_create', job_id: data.id %> <%= render 'job/link_create', job_id: data.id %>
<% end %> <% end %>
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
</div> </div>
</div> </div>
<div class="col-lg-1 col-md-1 col-sm-1"> <div class="col-lg-1 col-md-1 col-sm-1">
<%= link_to '<i class="fas fa-trash"></i>'.html_safe, destroy_favorite_path(favorite_id: data.favorites.ids), method: :delete, remote: true, class: 'link_favorite in_block' %> <%= link_to '<i class="fas fa-trash"></i>'.html_safe, favorite_path(data.favorites.ids), method: :delete, remote: true, class: 'link_favorite in_block' %>
</div> </div>
</div> </div>
</div> </div>
......
<%= link_to '<i class="far fa-heart"></i>'.html_safe, favorite_path(job_id: job_id), method: :post, remote: true, class: 'link_favorite' %> <%= link_to '<i class="far fa-heart"></i>'.html_safe, favorite_index_path(job_id: job_id), method: :post, remote: true, class: 'link_favorite' %>
<%= link_to t('pages.index.btn_text_favorite'), favorite_path(job_id: job_id), method: :post, remote: true, class: 'btn btn-apply_job_favorite' %> <%= link_to t('pages.index.btn_text_favorite'), favorite_index_path(job_id: job_id), method: :post, remote: true, class: 'btn btn-apply_job_favorite' %>
<%= link_to '<i class="fas fa-heart"></i>'.html_safe, destroy_favorite_path(favorite_id: favorite_id), method: :delete, remote: true, class: 'link_favorite' %> <%= link_to '<i class="fas fa-heart"></i>'.html_safe, favorite_path(favorite_id), method: :delete, remote: true, class: 'link_favorite' %>
<%= link_to t('pages.index.unfavorite'), destroy_favorite_path(favorite_id: favorite_id), method: :delete, remote: true, class: 'btn btn-apply_job_favorite unfavorite' %> <%= link_to t('pages.index.unfavorite'), favorite_path(favorite_id), method: :delete, remote: true, class: 'btn btn-apply_job_favorite unfavorite' %>
...@@ -10,11 +10,9 @@ ...@@ -10,11 +10,9 @@
<div class="header_top_menu_right"> <div class="header_top_menu_right">
<ul class="list_menu_header_right"> <ul class="list_menu_header_right">
<li class="list_item_menu_header_right"> <li class="list_item_menu_header_right">
<% if user_signed_in? %> <%= link_to_if(!user_signed_in?, t('pages.index.favorite'), favorite_index_path, class: 'link_item_menu_header_right number_favorite') do
<%= link_to "(#{current_user.favorites.count}) #{t('pages.index.favorite')}", favorite_index_path, class: 'link_item_menu_header_right number_favorite' %> link_to "(#{current_user.favorites.count}) #{t('pages.index.favorite')}", favorite_index_path, class: 'link_item_menu_header_right'
<% else %> end %>
<%= link_to t('pages.index.favorite'), favorite_index_path, class: 'link_item_menu_header_right' %>
<% end %>
</li> </li>
<li class="list_item_menu_header_right"> <li class="list_item_menu_header_right">
<%= link_to t('pages.index.history'), '#', class: 'link_item_menu_header_right' %> <%= link_to t('pages.index.history'), '#', class: 'link_item_menu_header_right' %>
...@@ -57,7 +55,9 @@ ...@@ -57,7 +55,9 @@
<div class = "menu_mobile_vertical"> <div class = "menu_mobile_vertical">
<ul class = "list_menu_mobile_vertical"> <ul class = "list_menu_mobile_vertical">
<li class = "item_menu_mobile_vertical"> <li class = "item_menu_mobile_vertical">
<%= link_to t('pages.index.favorite'), favorite_index_path, class: 'link_item_menu_mobile' %> <%= link_to_if(!user_signed_in?, t('pages.index.favorite'), favorite_index_path, class: 'link_item_menu_mobile') do
link_to "(#{current_user.favorites.count}) #{t('pages.index.favorite')}", favorite_index_path, class: 'link_item_menu_mobile number_favorite'
end %>
</li> </li>
<li class = "item_menu_mobile_vertical"> <li class = "item_menu_mobile_vertical">
<%= link_to t('pages.index.history'), '#', class: 'link_item_menu_mobile' %> <%= link_to t('pages.index.history'), '#', class: 'link_item_menu_mobile' %>
......
alert('You will be redirected to log in.');
window.location = '/users/sign_in';
\ No newline at end of file
...@@ -15,9 +15,7 @@ Rails.application.routes.draw do ...@@ -15,9 +15,7 @@ Rails.application.routes.draw do
post 'done', to: 'apply_job#done', as: :done post 'done', to: 'apply_job#done', as: :done
get 'my/jobs', to: 'apply_job#index', as: :list_applied_jobs get 'my/jobs', to: 'apply_job#index', as: :list_applied_jobs
# Fovorite # Fovorite
post 'favorite', to: 'favorite#create', as: :favorite resources :favorite, only: %i[index create destroy]
delete 'favorite', to: 'favorite#destroy', as: :destroy_favorite
get 'favorite', to: 'favorite#index', as: :favorite_index
# Details job # Details job
get 'detail/:id', to: 'job#detail', as: :detail_job get 'detail/:id', to: 'job#detail', as: :detail_job
# Search # Search
......
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