[英]rails change the background of my like button when clicks on it :
I'm in the last step of my rails application (instagram-clone ), and the issue is when i click on heart logo the count of number of likes upgraded from 0 to 1 but, i can't find the solution to change the background color of my logo to "red" when i click on it, this is my logo image我在我的rails应用程序(instagram-clone)的最后一步,问题是当我点击心脏标志时,喜欢的数量从0升级到1但是,我找不到改变的解决方案当我点击它时,我的标志的背景颜色变为“红色”,这是我的标志图像
this is my vote.js.erb:这是我的 vote.js.erb:
<% if current_user.liked? @post %>
$(".like-button").addClass("liked");
<% else %>
$(".like-button").removeClass("like-button");
<% end %>
$(".likes-count").html(" <%= @post.get_upvotes.size%> ")
#i want to modify this lines to change the background color of my heart logo to red when the count of likes was upgraded from 0 to 1 and thanks a lot
this is my like button code lines:这是我喜欢的按钮代码行:
<%= link_to like_post_path(post), class:"like-button", method: :put, remote: :true do %>
<div>
<i class="fas fa-heart fa-2x"></i>
</div>
<% end %>
<span class="likes-count"><%= post.get_upvotes.size %></span>
Ok here is your option.好的,这是您的选择。 There could be many stories/photos you might be looking and scrolling through the list.
您可能正在查看和滚动列表中的许多故事/照片。
Here is the fix that suit this kind of scenarios.这是适合这种情况的修复程序。
Some changes in vote.js.erb: vote.js.erb 的一些变化:
<% if current_user.liked? @post %>
$(".like-button-<%= @post.id %> .fas").addClass("liked");
<% else %>
$(".like-button-<%= @post.id %> .fas").removeClass("liked");
<% end %>
$(".likes-count").html(" <%= @post.get_upvotes.size%> ")
As you are using icon, Somewhere in the style add css similar to below.当您使用图标时,在样式中的某处添加类似于下面的 css。
.liked {
color: red;
}
And some changes in your html button以及您的 html 按钮的一些变化
<%= link_to like_post_path(post), class: "like-button-#{post.id}", method: :put, remote: :true do %>
<div>
<i class="fas fa-heart fa-2x <%= 'liked' if current_user.liked?(post) %>"></i>
</div>
<% end %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.