简体   繁体   English

当点击它时,rails会改变我的like按钮的背景:

[英]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.

相关问题 当用户单击按钮时,我希望我的ID晃动 - I would like my id to shake when the user clicks a button 想要在用户单击时像按钮一样将按钮更改为默认状态,反之亦然 - want to change like button to default state when user clicks unlike button and vice versa 检测用户何时“点击”Facebook 点赞按钮 - Detect when user “clicks” the Facebook like button 用户单击时更改输入的背景颜色 - Change background color of an input when user clicks 使用 if/else 语句时,按钮需要单击 2 次才能更改背景颜色 - Button takes 2 clicks to change background-color when using if/else statement 想要一个按钮来更改颜色并在用户单击时更改文本 - Would like a button to change color and change text as a user clicks Why does my like button return a Json object { liked: true } but doesn't work with my ajax call in Django when a user clicks the like button - Why does my like button return a Json object { liked: true } but doesn't work with my ajax call in Django when a user clicks the like button Rails-用户单击提交按钮时如何更新表中的数据 - Rails - How update data in table when user clicks on submit button 使用 reactjs 和 Material UI 单击按钮时更改内容 - Change content when clicks the button with reactjs & Material UI 用户单击扩展程序上的按钮时如何插入文本? - How to insert text when the user clicks a button on my extension?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM