gpt4 book ai didi

javascript - Rails Acts_as_votable ajax/js upvoting 所有帖子而不是一个

转载 作者:行者123 更新时间:2023-11-30 00:17:51 26 4
gpt4 key购买 nike

好吧,我开始在这个问题上大费周章了。我是 Rails 的新手,正在学习制作 pinterest 风格应用程序的教程。我完成了它,但对投票系统不满意。

每次我点击投票时都会刷新页面。所以我找到了一些关于它的帖子并完全按照它说的做了。它一直在加载一个页面,向我显示 js 代码而不是执行它。

今天早上,我将“put”更改为“get”,然后又改回来,现在它正在工作......不知道发生了什么改变它。

但是,现在它对页面上的每个帖子进行投票,而不仅仅是我点击的帖子。当我将鼠标悬停在链接上时,它们都指向该链接的正确 ID。

这是来自 Controller 的代码:

def upvote
@pin.upvote_by current_user
respond_to do |format|
format.html { redirect_to :back }
format.js { render :layout => false }
format.json
end
end

和 View (haml):

.btn-group.pull-right
= link_to like_pin_path(pin), method: :put, remote: true, class: "btn btn-like" do
%span.glyphicon.glyphicon-heart
= pin.get_upvotes.size

投票.js.erb:

$('.glyphicon-heart').html('<%=@pin.get_upvotes.size%>');

路线.rb

Rails.application.routes.draw do
devise_for :users
resources :pins do
member do
put "like", to: "pins#upvote", defaults: { format: 'js' }
end
end

root "pins#index"
end

这是点击赞按钮时来自控制台的日志:

Started PUT "/pins/10/like" for ::1 at 2015-12-08 11:40:02 -0600
Processing by PinsController#upvote as JS
Parameters: {"id"=>"10"}
Pin Load (0.2ms) SELECT "pins".* FROM "pins" WHERE "pins"."id" = ? LIMIT 1 [["id", 10]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 2]]
(0.3ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."voter_id" = ? AND "votes"."voter_type" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 10], ["votable_type", "Pin"], ["voter_id", 2], ["voter_type", "User"]]
ActsAsVotable::Vote Load (0.6ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."voter_id" = ? AND "votes"."voter_type" = ? AND "votes"."vote_scope" IS NULL ORDER BY "votes"."id" DESC LIMIT 1 [["votable_id", 10], ["votable_type", "Pin"], ["voter_id", 2], ["voter_type", "User"]]
(0.2ms) begin transaction
(0.1ms) commit transaction
(0.2ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 10], ["votable_type", "Pin"], ["vote_flag", "t"]]
Rendered pins/upvote.js.erb (4.4ms)
Completed 200 OK in 25ms (Views: 9.4ms | ActiveRecord: 1.8ms)

我确定这确实很简单,但 ajax/js 对我来说比 rails 还新。

让我知道是否还有其他需要发布的内容。任何帮助将不胜感激!

另外,为了将来引用,什么会使链接加载带有 js 代码的页面而不是执行代码?因为我不觉得我今天有什么大的改变,我不知道我是怎么熬过来的。我很高兴我做到了,但知道怎么做会很有帮助。

最佳答案

这样做是因为您的 JS 在技术上以该类的所有链接为目标。相反,您可以将模型的单个 pin 的 id 附加到链接的类或创建数据属性,但这完全取决于您。这样一来,它只会定位到被点击的一个链接。

更新:

本质上,您需要将一个类(实例的 ID)分配给包含投票按钮的容器 div:pin-#{pin.id} 用于索引 View pin-#{@pin.id} 用于显示 View

Controller

def upvote
@pin.upvote_by current_user

respond_to do |format|
format.js { render "upvote.js.erb" }
end
end

路线

...
resources :pins do
member do
put :upvote
end
end

索引 View

...
%div{class: "btn-group pull-right pin-#{pin.id}"}
= render "upvote", pin: pin

显示 View

...
.col-md-6
%div{class: "btn-group pull-right pin-#{@pin.id}"}
= render "upvote", pin: @pin
-if user_signed_in?
= link_to "Edit", edit_pin_path, class: "btn btn-default"
= link_to "Delete", pin_path, method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-default"

_upvote.html.haml

= link_to upvote_pin_path(pin), method: :put, remote: :true, class: "btn btn-default btn-like" do
%span.glyphicon.glyphicon-heart
= pin.get_upvotes.size

upvote.js.erb

$(".pin-<%= @pin.id %>").html("<%= escape_javascript(render 'upvote', pin: @pin) %>");

关于javascript - Rails Acts_as_votable ajax/js upvoting 所有帖子而不是一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34162562/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com