简体   繁体   English

点赞按钮赞所有帖子

[英]Like button likes all posts

I've implemented a like/unlike function in my app. 我已经在我的应用中实现了喜欢/不喜欢的功能。 On the show page for an item, you can like/unlike without any issues. 在项目的显示页面上,您可以喜欢/不喜欢,没有任何问题。 On a user's profile page though, where it lists all the foods they've uploaded, I have two issues: First, when I click the like button for one food, it triggers it for every food and then the button text under every food says "unlike this food", instead of only having the button text change for just the food that was clicked on. 但是,在用户的个人资料页面上,其中列出了他们上载的所有食物,我有两个问题:首先,当我单击一种食物的“赞”按钮时,它将触发每种食物,然后每种食物下方的按钮文本都显示“与这种食物不同”,而不是仅对单击的食物进行按钮文本更改。 The like gets saved to the db for the correct food, but obviously I don't want the button text to change for foods that I haven't liked. 喜欢的东西会保存到db中以获取正确的食物,但是显然我不希望按钮文字更改为我不喜欢的食物。 Second, when I try to like a different food on the page without refreshing, the liked gets saved in the db as the original food that I clicked on, instead of the one I actually clicked on. 其次,当我尝试不喜欢页面上的其他食物而没有刷新时,喜欢的食物将被保存为我单击的原始食物,而不是我实际单击的食物。

users.show.html.erb

<% @user.foods.each do |food| %>
    <div class="row col-md-12">
        <div class="food col-md-4">
            <h3 class="title"><%= link_to food.title.capitalize, food_path(food.id) %></h3>
            <%= link_to (image_tag (food.image.url)), food_path(food.id) %>
            <%= render 'shared/vote_form', :food => food %>

            <%= link_to pluralize(food.votes.count, "person"), user_votes_path(:user_id => current_user.id, :food_id => food.id) %> <%= food.votes.count == 1 ? 'wants' : 'want' %> to gobble this
        </div>

        <div class="description col-md-6">
        <% if @user.id == current_user.id %>

                <% if food.description.length == 0 %>
                    <p><%= link_to "Add Description", edit_food_path(food) %></p>   
                <% else %>
                    <p><%= food.description %><p>
            <p><%= link_to "Edit Description", edit_food_path(food) %></p>
                <% end %>

          <% if food.recipe.length == 0 %>
            <p><%= link_to "Add Recipe", edit_food_path(food) %></p>
          <% end %>

        <% else %>

          <% if food.description.length == 0 %>
            <p>No Description</p> 
          <% else %>
            <p><%= food.description %><p>
          <% end %>

          <% if food.recipe.length == 0 %>
            <p>No Recipe</p>
          <% else %>
            <p><%= link_to "View Recipe", food_path(food) %></p>  
          <% end %>

        <% end %>
        </div>
    </div>
<% end %>

votes controller 投票控制人

class VotesController < ApplicationController

  def index
    @votes = Food.find(params[:food_id]).votes
  end

  def new
    @vote = current_user.votes.new
  end

  def create
    @user = current_user
    @vote = current_user.votes.build(vote_params)


    if @vote.save!
      @food = @vote.food
      respond_to do |format|
        format.html {redirect_to :back, notice: "Liked!"}
        format.js  
      end

      puts @vote.food.id
    else
      puts "No"
      redirect_back(fallback_location: root_path)
    end
  end

  def show
    @user = current_user
    @vote = Vote.find(params[:id])
  end

  def destroy
    @vote = Vote.find(params[:id])
    @food = @vote.food

    if @vote.destroy!
      respond_to do |format|
        format.html {redirect_to :back, notice: "Unliked!"}
        format.js 
      end
    else
      puts "NOOOOOO"
    end
  end

  private

  def vote_params
    params.require(:vote).permit(:food_id)
  end
end

vote partial

<% unless current_user.votes.pluck(:food_id).include?(food.id) %>
    <%= button_to "Like This Food", user_votes_path(current_user, { vote: { food_id: food.id } }), :remote => true %>
<% else %>
    <% vote = food.votes.where(user_id: current_user.id).first %>
    <%= button_to "Unlike This Food", user_vote_path(current_user, vote), :remote => true, method: "delete" %>
<% end %>

create.js.erb

$('.button_to').replaceWith("<%= j (render :partial => 'shared/vote_form', :locals => { :food => @food, :food_id => @food.id }) %>");

destroy.js.erb

$('.button_to').replaceWith("<%= j (render :partial => 'shared/vote_form', :locals => { :food => @food }) %>");

Take a look at your create.js.erb ( destroy.js.erb has the same problem). 看一下您的create.js.erbdestroy.js.erb也有同样的问题)。 You use select all elements with the class button_to - that is all buttons on the page. 您可以将所有元素都与class button_to -即页面上的所有按钮。 Then you call replaceWith that replaces all these buttons with the button provided as the argument. 然后,调用replaceWith ,将所有这些按钮替换为作为参数提供的按钮。 The result is that all buttons get replaced with the like button corresponding to the food you just liked. 结果是所有按钮都被替换为与您喜欢的食物相对应的like按钮。 This is responsible for affecting all buttons on the page and making them refer the food you liked first. 这负责影响页面上的所有按钮,并使它们首先引用您喜欢的食物。

The solution is to add an id attribute to the buttons that would include the ID of the food the button pertains to. 解决方案是在按钮上添加一个id属性,其中应包含该按钮所属食品的ID。 For example, if Food with ID 1 is Lettuce then you need to add id="like-1" to the button and change $('.button_to') to $("#like-1") . 例如,如果ID为1的FoodLettuce则需要向按钮添加id="like-1"并将$('.button_to')更改$("#like-1")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM