I was wondering how to determine what page a :delete request is coming from? For example, I have a wall post that appears on both a user’s home page and show page. When a user deletes a post from the home page, I would want the user to be redirected to the home page while if he deleted it from his show (profile) page, I would want the user to get redirected back there. The issue is though, I’m having trouble differentiating where its coming from.
I understand that in :delete requests, you can’t pass in hidden values since it’s not a :post. I’ve tried checking the params, but they all wind up being the same. They have the same :method, :controller, and :action i.e.
{"_method"=>"delete", "authenticity_token"=>"xNsfq27sBrpssTO8sk0aAzzIu8cvnFJEZ30c17Q+BCM=",
"action"=>"destroy", "controller"=>"pub_messages", "id"=>"33"}
In my destroy action, I have:
def destroy
@pub_message = PubMessage.find_by_id(params[:id])
@pub_message.destroy
redirect_to user_path(@pub_message.to_id)
end
But instead of always redirecting back to the user_path, I want to redirect to the root_path but just can’t figure out when the user is issuing the destroy action while on the home page or not.
Where I show the delete option in my view is…
<% if current_user == feed_item.user or current_user == feed_item.to %>
<%= link_to "delete", feed_item, method: :delete,
confirm: "You sure?",
title: feed_item.content %>
<% end %>
How can I fix this?
You can
redirect_to :back, as described here. That will take you back to the HTTP_REFERER from the request.