Reputation: 349
I have a link to a custom destroy action that deletes a record through an AJAX call. The issue is, when I click the link_to button. I receive ArgumentError (wrong number of arguments (given 0, expected 1)): Normally, it would work without issue. But, I am unable to recognize what the cause of the error is. The record that I am searching for, is associated with the current object in scope. How do I successfully destroy records with the unshare action given the parameters?
I tried changing the action's parameters to find the record in question and delete the object. Only one approach worked, and that was with a where() clause, that allowed me to find the object with the original_post id and current_user.id. Lastly, I created an unshare.js.erb, which correlates with the action.
_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
Expected Results: When you click unshare, the destroy action initiates successfully and the post fades away.
Actual Results: I receive an error stating that the destroy action requires 1 parameter, but 0 was given in 'block in unshare'.
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'
UPDATE SERVER LOG
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"):
UPDATE 2
posts_controller/share action
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}) %>');
Upvotes: 1
Views: 2606
Reputation: 2397
Post.where
returns collection of records, so that means post
variable contains multiple records.
you have to call post.delete_all
or call #destroy
method to each post instance, for example: post.each {|p| p.destroy }
however to fix your code, it should be
def unshare
post = Post.find_by(original_post_id: @post.original_post_id, user_id: current_user.id)
if post.blank?
respond_to do |format|
if post.destroy
format.html { redirect_to :back, notice: "Post is not found" }
end
end
return
end
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
Upvotes: 1