diff --git a/app/assets/javascripts/resources.js b/app/assets/javascripts/resources.js index 6e95670..e2b7f88 100644 --- a/app/assets/javascripts/resources.js +++ b/app/assets/javascripts/resources.js @@ -29,20 +29,92 @@ function scrollToTop(event) { $('html, body').animate({scrollTop: 0}, 'slow'); } -function incrementLike(event, target) { +//getCookie function taken from http://www.w3schools.com/js/js_cookies.asp +function getCookie(cname) { + var name = cname + "="; + var ca = document.cookie.split(';'); + for(var i = 0; i = 0){ + counterEl.innerText = newCount; + } + + if (newCount === 0) { + button.removeClass('filled-heart'); + button.addClass('heart'); + } if (newCount === 1) { - var button = form.find('button'); button.addClass('filled-heart'); - } + } +} - $.ajax(form.attr("action"), { - type: "POST" - }); -} \ No newline at end of file diff --git a/app/controllers/resources_controller.rb b/app/controllers/resources_controller.rb index d439db6..89715b3 100644 --- a/app/controllers/resources_controller.rb +++ b/app/controllers/resources_controller.rb @@ -58,7 +58,7 @@ def update end def like - @resource.likes += 1 + @resource.likes += params[:count].to_i @resource.save redirect_to resources_path end diff --git a/app/views/resources/index.html.erb b/app/views/resources/index.html.erb index b64f348..4f3577e 100644 --- a/app/views/resources/index.html.erb +++ b/app/views/resources/index.html.erb @@ -49,7 +49,7 @@
<%= format_date(resource.date) %>
@@ -97,7 +97,7 @@ // Ajax Like $(".heart").click(function(e){ - incrementLike(e, this); + processLike(e, this); }); }); diff --git a/config/routes.rb b/config/routes.rb index 04bb352..e84cd87 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,7 +5,7 @@ resources :resources, only: [:index] get 'resources/tags/:tag', to: 'resources#index', as: :tag - post 'resources/:id/like', to: 'resources#like', as: :like + post 'resources/:id/like/:count', to: 'resources#like', as: :like # Static pages get 'about', to: "static_pages#about"