Damir Nurgaliev Damir Nurgaliev - 5 days ago 5
Ruby Question

How to make the rails link_to include other tags

i'm creating a like model, so here's a code:

- if policy(bonus).liked_by?
= link_to(image_tag("heart--filled--green.png", class: "Dislike"),
bonus_like_path(bonus, bonus.user_like(current_user)), method: :delete,
data: { remote: true, behavior: "fragments" })
- else
= link_to(image_tag("heart.svg", class: "Like"),
bonus_likes_path(bonus), method: :post,
data: { remote: true, behavior: "fragments" })
- if bonus.likes_count.zero?
span Like
-else
span.has-tip data-tooltip="" title="#{ bonus.liked_by }" Like
span class="like_count" #{ bonus.likes_count }


And it generates something like this:

enter image description here

The problem is that if I want to like something, I would like to press on the heart(like the given image), but I need to give the opportunity to press everywhere including the span Like and like's count. How can I solve my problem?

Answer

To make the image along with the spans part of the link, wrap them inside link_to using a block.

= link_to bonus_likes_path(bonus), method: :post, data: { remote: true, behavior: "fragments" } do
  = image_tag("heart.svg", class: "Like"
  - if bonus.likes_count.zero?
    span Like
  - else
    span.has-tip data-tooltip="" title="#{ bonus.liked_by }" Like
  span class="like_count" #{ bonus.likes_count }
Comments