【发布时间】:2019-02-10 01:23:13
【问题描述】:
我有一个自定义销毁操作的链接,该操作通过 AJAX 调用删除记录。问题是,当我单击 link_to 按钮时。我收到 ArgumentError(错误数量的参数(给定 0,预期为 1)):通常,它可以正常工作。但是,我无法识别错误的原因是什么。我正在搜索的记录与范围内的当前对象相关联。在给定参数的情况下,如何使用取消共享操作成功销毁记录?
我尝试更改操作的参数以查找有问题的记录并删除对象。只有一种方法有效,那就是使用 where() 子句,它允许我找到具有 original_post id 和 current_user.id 的对象。最后,我创建了一个与操作相关的 unshare.js.erb。
_post.html.erb
<%= link_to unshare_post_path(post), method: :delete, remote: true, style: 'text-decoration: none;' do %>
<i class="fas fa-share-square fa-2x post-charm-bar-icon-color"></i>
<% end %>
unshare.js.erb
$('#share_<%= @post.id %>').html('<%= escape_javascript(render :partial => "posts/share", :locals => {:post => @post}) %>');
$("#share_<%= @post.id %>").fadeOut("slow", function () {
$(this).remove();
});
posts_controller.rb
def unshare
post = Post.where(original_post_id: @post.id).where(user_id: current_user.id)
respond_to do |format|
if post.destroy
format.html { redirect_to posts_path, notice: "You've successfully unshared the Post" }
format.js
end
end
end
routes.rb
resources :posts, on: :collection do
member do
post :share
delete :unshare
end
end
预期结果:当您点击取消分享时,销毁操作成功启动,帖子消失。
实际结果:我收到一条错误消息,指出销毁操作需要 1 个参数,但在 'block in unshare' 中给出了 0。
Started DELETE "/posts/472epAQhoQfg/unshare" for 127.0.0.1 at 2019-02-09 19:22:02 -0500
Processing by PostsController#unshare as JS
Parameters: {"on"=>:collection, "id"=>"472epAQhoQfg"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1
ORDER BY "users"."id" ASC LIMIT $2 [["id", 2], ["LIMIT", 1]]
Post Load (1.0ms) SELECT "posts".* FROM "posts" WHERE "posts"."hash_id" = $1 LIMIT $2 [["hash_id", "472epAQhoQfg"], ["LIMIT", 1]]
Completed 500 Internal Server Error in 10ms (ActiveRecord: 2.0ms)
ArgumentError (wrong number of arguments (given 0, expected 1)):
app/controllers/posts_controller.rb:70:in `block in unshare'
app/controllers/posts_controller.rb:69:in `unshare'
更新服务器日志
Started GET "/new_notification_check.json" for 127.0.0.1 at 2019-02-09 20:40:12 -0500
Started DELETE "/posts/472epAQhoQfg/unshare" for 127.0.0.1 at 2019-02-09 20:40:13 -0500
Processing by NotificationsController#check_for_new_notifications as JSON
Processing by PostsController#unshare as JS
Parameters: {"on"=>:collection, "id"=>"472epAQhoQfg"}
User Load (3.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1
ORDER BY "users"."id" ASC LIMIT $2 [["id", 2], ["LIMIT", 1]]
User Load (3.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1
ORDER BY "users"."id" ASC LIMIT $2 [["id", 2], ["LIMIT", 1]]
Notification Load (2.0ms) SELECT "notifications".* FROM "notifications"
WHERE "notifications"."recipient_id" = $1 AND "notifications"."read_at" IS NULL [["recipient_id", 2]]
Completed 200 OK in 10ms (Views: 1.8ms | ActiveRecord: 5.0ms)
Post Load (7.0ms) SELECT "posts".* FROM "posts" WHERE "posts"."hash_id" = $1 LIMIT $2 [["hash_id", "472epAQhoQfg"], ["LIMIT", 1]]
Post Load (1.0ms) SELECT "posts".* FROM "posts" WHERE "posts"."original_post_id" IS NULL AND "posts"."user_id" = $1 LIMIT $2 [["user_id", 2], ["LIMIT", 1]]
(1.0ms) BEGIN
Impression Destroy (493.9ms) DELETE FROM "impressions" WHERE "impressions"."impressionable_id" = $1 AND "impressions"."impressionable_type" = $2 [["impressionable_id", 1], ["impressionable_type", "Post"]]
ActsAsVotable::Vote Load (4.0ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 [["votable_id", 1], ["votable_type", "Post"]]
Comment Load (3.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."commentable_id" = $1 AND "comments"."commentable_type" = $2 AND "comments"."parent_id" IS NULL [["commentable_id", 1], ["commentable_type", "Post"]]
Comment Load (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 [["post_id", 1]]
Post Destroy (1.0ms) DELETE FROM "posts" WHERE "posts"."id" = $1 [["id", 1]]
(38.0ms) COMMIT
Redirected to http://127.0.0.1:3000/posts
Completed 302 Found in 701ms (ActiveRecord: 554.9ms)
Started DELETE "/posts" for 127.0.0.1 at 2019-02-09 20:40:16 -0500
ActionController::RoutingError (No route matches [DELETE] "/posts"):
更新 2
posts_controller/share 操作
def share
post = current_user.posts.new(original_post_id: @post.id)
if post.save
respond_to do |format|
format.html {redirect_to :back}
format.js {render action: 'share'}
end
end
end
share.js.erb
$('#share_<%= @post.id %>').html('<%= escape_javascript(render :partial => "posts/share", :locals => {:post => @post}) %>');
【问题讨论】:
-
搜索完我的路线后,我看到了 PATH:/posts/:id/unshare。 操作:posts#unshare。 路径网址:unshare_post
-
你如何在 unshare 方法中设置
@post?哪里会返回一个 ActiveRecord::Relation,所以你必须拉出你想要的帖子。也许通过将 .first 链接到调用。 -
我更新了问题正文。更新 2 显示了共享操作是如何实现的。它可以工作,但不会呈现 share.js.erb 响应。就像用新的按钮状态刷新视图并更改为计数一样。
-
但是你如何在取消分享操作中设置
@post?我看到您正在使用@post,但您必须在某处设置@post。您是否使用 before_action 来设置它? -
在显示视图中。正在使用@post 变量。我应该删除帖子上的@ 并将其保留为 post 吗? before_action :set_post, only: %i[show edit update destroy share unshare ]。顺便说一句,我会再试一次,不要在 before_action 中分享 unshare。
标签: javascript ruby-on-rails ruby ajax