Damir Nurgaliev
Damir Nurgaliev

Reputation: 351

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?

Upvotes: 0

Views: 111

Answers (2)

Arun Kumar Mohan
Arun Kumar Mohan

Reputation: 11915

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 }

Upvotes: 3

VishalTheBeast
VishalTheBeast

Reputation: 484

You can have a bigger block in link_to using:

<%= link_to desired_path do %>
   <div class="class-name">

   </div>
<% end %>

Upvotes: 0

Related Questions