我在我的应用中实现了喜欢/不同的功能。在项目的显示页面上,您可以喜欢/不同而没有任何问题。在一个用户的个人资料页面上,它列出了他们上传的所有食物,我有两个问题:首先,当我点击一个食物的相似按钮时,它会触发每个食物,然后是每种食物下面的按钮文字都说"不像这种食物"而不仅仅是点击食物的按钮文字改变了。这样的东西被保存到数据库中以获得正确的食物,但显然我不希望按钮文本改变为我不喜欢的食物。其次,当我尝试在页面上尝试不同的食物而没有刷新时,喜欢被保存在数据库中作为我点击的原始食物,而不是我实际点击的那个。
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 %>
投票控制员
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 }) %>");
答案 0 :(得分:1)
查看您的create.js.erb
(destroy.js.erb
有同样的问题)。您可以使用类button_to
选择所有元素 - 即页面上的所有按钮。然后使用提供的按钮作为参数调用replaceWith
来替换所有这些按钮。结果是所有按钮都被替换为与您喜欢的食物相对应的类似按钮。这有助于影响页面上的所有按钮,并让他们首先参考您喜欢的食物。
解决方案是在按钮中添加id
属性,其中包含按钮所属食物的ID。例如,如果ID为1的Food
为Lettuce
,则您需要将id="like-1"
添加到该按钮,并将$('.button_to')
更改为$("#like-1")
。