So I've seen that other people have been confused me about this, and I have read other answers but I'm still confused.
How is information stored and parsed into the params hash during a delete request? I understand how it works in regards to submitting information to it. i.e. when a Put, Post or Get request is issued, I understand that the info is passed via params hash to respective controller action.
However, based on the code below in the user partial (_user.html.erb) :
<li>
<%= gravatar_for user, size: 52 %>
<%= link_to user.name, user %>
<% if current_user.admin? && !current_user?(user) %>
<%= link_to "delete", user, method: delete,
data: {confirm: "You sure?"} %>
<% end %>
</li>
And the code in the DESTROY action that is automatically routed to:
def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to users_url
end
I don't understand how the params hash gets the user id stored in it. I'd understand if it were params[:user][:id] since we are posting the user which has its list of attributes. But, I don't get how the id gets stored DIRECTLY into the params hash. This has been bothering me for a while so please any insight would be appreciated.
It's the way how rails works. If you do a rake routes
you will see that you get something like DELETE /users/:id
.
This means that the user object that you pass as the parameter gets interpreted as :id
. When Rails sees this, it will know that it has to look at the ID of the object passed, by convention all the models do have an ID field.
It's based on rails routing model.
If you used resources :user in config/routes.db it will create the following route (you can view it by running rake routes) :
DELETE users/:id
the symbol :id means that whatever you add after users/ while calling url will be set in params as :id. (in the case, for the DELETE HTTP verb)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With