I'm using the acts_as_votable gem to add a Like button to my posts. I followed the instructions I found in the following StackOverflow question:
Like button Ajax in Ruby on Rails
The code works really well but I'd rather use an an image than the text link. Basically I'm trying to use an existing design template that uses the following code based on like or dislike:
Like
<span class="heart-icon" style="background-position: 0px -290px;" id="<%= @pride.id %>"></span>
Dislike
<span class="heart-icon" style="background-position: 0px -256px;" data-id="<%= @pride.id %>"></span>
I tried using a do on the link_to and it works but also applies a text overlay that I believe is coming from the JS file. I've searched though a lot of the questions on here and I can't seem to find one that fits my situation. I'm hoping someone in the community will be able to help me out.
Here is how I currently have it setup:
My view looks like this:
<% if current_user.liked? @content %>
<%= link_to "Dislike", dislike_content_path(@content), class: 'vote', method: :put, remote: true, data: { toggle_text: 'Like', toggle_href: like_content_path(@content), id: @content.id } %>
<% else %>
<%= link_to "Like", like_content_path(@content), class: 'vote', method: :put, remote: true, data: { toggle_text: 'Dislike', toggle_href: dislike_content_path(@content), id: @content.id } %>
<% end %>
My CoffeeScript looks like this:
$(document).on 'ajax:success', 'a.vote', (status,data,xhr)->
# update counter
$(".votes-count[data-id=#{data.id}]").text data.count
# toggle links
$("a.vote[data-id=#{data.id}]").each ->
$a = $(this)
href = $a.attr 'href'
text = $a.text()
$a.text($a.data('toggle-text')).attr 'href', $a.data('toggle-href')
$a.data('toggle-text', text).data 'toggle-href', href
return
return
This is how I want my view configured but it keeps writing the Like/Dislike text over the image and I can't figure out where to change it.
<% if current_user.liked? @content %>
<%= link_to "Dislike", dislike_content_path(@content), class: 'vote', method: :put, remote: true, data: { toggle_text: 'Like', toggle_href: like_content_path(@content), id: @content.id } do %>
<span class="heart-icon" style="background-position: 0px -290px;" id="<%= @content.id %>"></span>
<% end %>
<% else %>
<%= link_to "Like", like_content_path(@content), class: 'vote', method: :put, remote: true, data: { toggle_text: 'Dislike', toggle_href: dislike_content_path(@content), id: @content.id } do %>
<span class="heart-icon" style="background-position: 0px -256px;" data-id="<%= @content.id %>"></span>
<% end %>
<% end %>