Commit ba345db2 by Thanh Hung Pham

Fix comment - change javascript to remote:true

parent 0226f7a2
$(function() { $(function() {
$( ".favorite_add" ).on( "click", function() { // $( ".favorite_add" ).on( "click", function() {
$.ajax({ // $.ajax({
url: "/jobs/favorite", // url: "/jobs/favorite",
type: "GET", // type: "GET",
data: {"job_id" : $("#job_id").val()}, // data: {"job_id" : $("#job_id").val()},
dataType: "json", // dataType: "json",
success: function(data) { // success: function(data) {
window.location.reload(); // window.location.reload();
} // }
}); // });
}); // });
$(".favorite_remove").on("click", function() { $(".favorite_remove").on("click", function() {
$.ajax({ $.ajax({
......
...@@ -51,12 +51,14 @@ class JobsController < ApplicationController ...@@ -51,12 +51,14 @@ class JobsController < ApplicationController
end end
def favorite def favorite
job = Job.find(params[:job_id]) @job_id = params[:job_id]
job = Job.find(@job_id)
Favorite.new(user: current_user, job: job).save if Favorite.where(user: current_user, job: job).blank? Favorite.new(user: current_user, job: job).save if Favorite.where(user: current_user, job: job).blank?
end end
def favorite_remove def favorite_remove
job = Job.find(params[:job_id]) @job_id = params[:job_id]
job = Job.find(@job_id)
Favorite.where(user: current_user, job: job).destroy_all Favorite.where(user: current_user, job: job).destroy_all
end end
......
$("#favorite_<%= @job_id %>").remove()
$("#favorite_remove_<%= @job_id %>").remove()
...@@ -12,13 +12,13 @@ ...@@ -12,13 +12,13 @@
</thead> </thead>
<tbody> <tbody>
<%- @favorited_jobs.each do |favorite| -%> <%- @favorited_jobs.each do |favorite| -%>
<tr> <tr id="favorite_remove_<%= favorite.job.id %>">
<td> <%= link_to favorite.job.name, job_path(favorite.job.id) %> </td> <td> <%= link_to favorite.job.name, job_path(favorite.job.id) %> </td>
<td> <%= truncate(favorite.job.description, length: 250) %> </td> <td> <%= truncate(favorite.job.description, length: 250) %> </td>
<td> <%= favorite.job.city.try(:name) %> </td> <td> <%= favorite.job.city.try(:name) %> </td>
<td> <%= favorite.job.salary %> </td> <td> <%= favorite.job.salary %> </td>
<td> <td>
<%= link_to 'Remove', '#', class: 'favorite_remove' %> <%= link_to 'Remove', jobs_favorite_remove_path(job_id: favorite.job.id, format: 'js'), remote: true, class: 'favorite_remove' %>
<input type="hidden" id="job_id" name="job_id" value="<%= favorite.job.id %>"> <input type="hidden" id="job_id" name="job_id" value="<%= favorite.job.id %>">
</td> </td>
</tr> </tr>
......
...@@ -35,9 +35,9 @@ ...@@ -35,9 +35,9 @@
<td> <%= job.salary %> </td> <td> <%= job.salary %> </td>
<td> <td>
<%- if Favorite.where(user: current_user, job: job).blank? -%> <%- if Favorite.where(user: current_user, job: job).blank? -%>
<div class="col-md-3"> <div class="col-md-3" id="favorite_<%= job.id %>">
<%- if user_signed_in? -%> <%- if user_signed_in? -%>
<%= link_to 'Favorite', '#', class: 'favorite_add' %> <%= link_to 'Favorite', jobs_favorite_path(job_id: job.id, format: 'js'), remote: true, class: 'favorite_add' %>
<%- else -%> <%- else -%>
<%= link_to 'Favorite', new_user_session_path %> <%= link_to 'Favorite', new_user_session_path %>
<%- end -%> <%- end -%>
......
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