Commit 52e930fe by Thanh Hung Pham

Add favorite feature

parent c6aba513
...@@ -21,6 +21,7 @@ gem 'uglifier', '>= 1.3.0' ...@@ -21,6 +21,7 @@ gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views # Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2' gem 'coffee-rails', '~> 4.2'
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5' gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
......
...@@ -91,6 +91,10 @@ GEM ...@@ -91,6 +91,10 @@ GEM
jbuilder (2.7.0) jbuilder (2.7.0)
activesupport (>= 4.2.0) activesupport (>= 4.2.0)
multi_json (>= 1.2) multi_json (>= 1.2)
jquery-rails (4.3.1)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
listen (3.1.5) listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4) rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7) rb-inotify (~> 0.9, >= 0.9.7)
...@@ -210,6 +214,7 @@ DEPENDENCIES ...@@ -210,6 +214,7 @@ DEPENDENCIES
devise devise
figaro figaro
jbuilder (~> 2.5) jbuilder (~> 2.5)
jquery-rails
listen (>= 3.0.5, < 3.2) listen (>= 3.0.5, < 3.2)
mysql2 (>= 0.3.18, < 0.5) mysql2 (>= 0.3.18, < 0.5)
puma (~> 3.7) puma (~> 3.7)
......
...@@ -12,5 +12,7 @@ ...@@ -12,5 +12,7 @@
// //
//= require rails-ujs //= require rails-ujs
//= require turbolinks //= require turbolinks
//= require jquery
//= require jquery_ujs
//= require_tree . //= require_tree .
//= require bootstrap //= require bootstrap
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
$(function() {
$(".favorite_add").click(function(event){
$.ajax({
url: "/jobs/favorite",
type: "POST",
data: {"job_id" : $("#job_id").val()},
dataType: "json",
success: function(data) {
}
});
});
});
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
...@@ -20,10 +20,7 @@ class JobsController < ApplicationController ...@@ -20,10 +20,7 @@ class JobsController < ApplicationController
end end
def favorite def favorite
if user_signed_in? job = Job.find(params[:job_id])
Favorite.new(user: current_user, job: job).save if Favorite.where(user: current_user, job: job).blank?
else
redirect_to new_user_session_path
end
end end
end end
...@@ -28,9 +28,16 @@ ...@@ -28,9 +28,16 @@
<%= job.city.name unless job.city.nil? %> <%= job.city.name unless job.city.nil? %>
<%= job.salary %> <%= job.salary %>
</div> </div>
<div class="col-md-3"> <%- if Favorite.where(user: current_user, job: job).blank? -%>
<%= link_to 'Favorite', jobs_favorite_path(job_id: job.id) %> <div class="col-md-3">
</div> <%- if user_signed_in? -%>
<%= link_to 'Favorite', '#', class: 'favorite_add' %>
<%- else -%>
<%= link_to 'Favorite', new_user_session_path %>
<%- end -%>
<input type="hidden" id="job_id" name="job_id" value="<%= job.id %>">
</div>
<%- end -%>
</div> </div>
<%- end -%> <%- end -%>
......
...@@ -8,6 +8,7 @@ Rails.application.routes.draw do ...@@ -8,6 +8,7 @@ Rails.application.routes.draw do
get 'jobs/applied_jobs', to: 'jobs#applied_jobs' get 'jobs/applied_jobs', to: 'jobs#applied_jobs'
get 'jobs/show/', to: 'jobs#show' get 'jobs/show/', to: 'jobs#show'
get 'jobs/favorite', to: 'jobs#favorite' get 'jobs/favorite', to: 'jobs#favorite'
post 'jobs/favorite', to: 'jobs#favorite'
resource :cities resource :cities
devise_scope :user do devise_scope :user do
......
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