title: Like button likes all posts
body: <p>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. 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.</p>
  
  <pre><code>users.show.html.erb
  
  &lt;% @user.foods.each do |food| %&gt;
      &lt;div class="row col-md-12"&gt;
          &lt;div class="food col-md-4"&gt;
              &lt;h3 class="title"&gt;&lt;%= link_to food.title.capitalize, food_path(food.id) %&gt;&lt;/h3&gt;
              &lt;%= link_to (image_tag (food.image.url)), food_path(food.id) %&gt;
              &lt;%= render 'shared/vote_form', :food =&gt; food %&gt;
  
              &lt;%= link_to pluralize(food.votes.count, "person"), user_votes_path(:user_id =&gt; current_user.id, :food_id =&gt; food.id) %&gt; &lt;%= food.votes.count == 1 ? 'wants' : 'want' %&gt; to gobble this
          &lt;/div&gt;
  
          &lt;div class="description col-md-6"&gt;
          &lt;% if @user.id == current_user.id %&gt;
  
                  &lt;% if food.description.length == 0 %&gt;
                      &lt;p&gt;&lt;%= link_to "Add Description", edit_food_path(food) %&gt;&lt;/p&gt;   
                  &lt;% else %&gt;
                      &lt;p&gt;&lt;%= food.description %&gt;&lt;p&gt;
              &lt;p&gt;&lt;%= link_to "Edit Description", edit_food_path(food) %&gt;&lt;/p&gt;
                  &lt;% end %&gt;
  
            &lt;% if food.recipe.length == 0 %&gt;
              &lt;p&gt;&lt;%= link_to "Add Recipe", edit_food_path(food) %&gt;&lt;/p&gt;
            &lt;% end %&gt;
  
          &lt;% else %&gt;
  
            &lt;% if food.description.length == 0 %&gt;
              &lt;p&gt;No Description&lt;/p&gt; 
            &lt;% else %&gt;
              &lt;p&gt;&lt;%= food.description %&gt;&lt;p&gt;
            &lt;% end %&gt;
  
            &lt;% if food.recipe.length == 0 %&gt;
              &lt;p&gt;No Recipe&lt;/p&gt;
            &lt;% else %&gt;
              &lt;p&gt;&lt;%= link_to "View Recipe", food_path(food) %&gt;&lt;/p&gt;  
            &lt;% end %&gt;
  
          &lt;% end %&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <hr>
  
  <p>votes controller </p>
  
  <pre><code>class VotesController &lt; 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
  </code></pre>
  
  <hr>
  
  <pre><code>vote partial
  
  &lt;% unless current_user.votes.pluck(:food_id).include?(food.id) %&gt;
      &lt;%= button_to "Like This Food", user_votes_path(current_user, { vote: { food_id: food.id } }), :remote =&gt; true %&gt;
  &lt;% else %&gt;
      &lt;% vote = food.votes.where(user_id: current_user.id).first %&gt;
      &lt;%= button_to "Unlike This Food", user_vote_path(current_user, vote), :remote =&gt; true, method: "delete" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <hr>
  
  <pre><code>create.js.erb
  
  $('.button_to').replaceWith("&lt;%= j (render :partial =&gt; 'shared/vote_form', :locals =&gt; { :food =&gt; @food, :food_id =&gt; @food.id }) %&gt;");
  </code></pre>
  
  <hr>
  
  <pre><code>destroy.js.erb
  
  $('.button_to').replaceWith("&lt;%= j (render :partial =&gt; 'shared/vote_form', :locals =&gt; { :food =&gt; @food }) %&gt;");
  </code></pre>