Why is the URL “id” element not part of the params objects after
submitting a form via form_tag?
Steps:
- Go to URL http://localhost:3000/users/delete?id=45
- Press Submit button
- Read id param in order to delete user, then redirect form
Delete Confirmation
<%= form_tag(‘/users/delete’) do %>
<%= submit_tag ‘Click here to delete this user’ %>
<% end %>
def delete
if request.post?
if (!params[:id].nil?)
@user=User.find(params[:id])
@user.delete
redirect_to(:controller=>‘users’, :action => ‘list’)
end
end
end
When the form is posted, params[:id] does not exist, why?
Thanks
Rodrigo L. wrote in post #1114532:
<%= form_tag(’/users/delete’) do %>
<%= submit_tag ‘Click here to delete this user’ %>
<% end %>
When the form is posted, params[:id] does not exist, why?
Because you’re using for_tag instead of form_for:
<%= form_for :user, :method => :delete do |f| %>
Note: The above is just an example. Normally you would not use a form at
all to request that a given model be destroyed. Below is an proper
example of deleting a given user:
<%= link_to ‘Destroy’, user, method: :delete, data: { confirm: ‘Are you
sure?’ } %>
Besides that I don’t see how your form_tag would ever send anything
about the current user. It’s not passing in the user, nor user id, at
all. It’s just submitting to the /users/delete URL with no other
information.
Thanks Robert, You just gave some insight. You’re right I should use
form_for, but I just want to point that the user id is indeed passed in
the URL above and I can reference a Model object even when not using a
form_for. But I don’t understand why it is not part of the
“request” object querystring parameters? Any Framework should be able to
capture both POST an URL parameters after a form submit action. How
about environmental parameters such as referrer, etc.? I just doesn’t
make sense to me. I guess I will just keep reading more about rails.
Rod
Rodrigo L. wrote in post #1114538:
Thanks Robert, You just gave some insight. You’re right I should use
form_for, but I just want to point that the user id is indeed passed in
the URL above and I can reference a Model object even when not using a
form_for. But I don’t understand why it is not part of the
“request” object querystring parameters? Any Framework should be able to
capture both POST an URL parameters after a form submit action. How
about environmental parameters such as referrer, etc.? I just doesn’t
make sense to me. I guess I will just keep reading more about rails.
<%= form_tag(’/users/delete’) do %>
<%= submit_tag ‘Click here to delete this user’ %>
<% end %>
The form_tag helper defaults to a POST request if method is not
specified. Form fields will be pass in the request body not in the URL
query string. I don’t see any indication that you’re passing any form
data in your example above. It’s not part of the URL and there are no
hidden fields that might contain any form data.
Example:
form_tag(’/posts’)
=>
Check your logs/development.log to see exactly what the params hash
contains for your request.
As for what you call “environmental parameters” I assume you mean
request headers. Request headers are available in the request object but
are not mixed into the params hash. That hash is reserved for user
submitted data (i.e. form fields and query parameters).
On Jul 5, 2013, at 7:03 PM, Rodrigo L. wrote:
Sorry, I just come from .net and I am used to Request.Querystring[]
which is able to fetch any URL parameter on post. The Request object, in
this case, takes care storing the URL parameter values. One other idea
you just gave me is to create an input field which would obtain the
value from the URL. I mean it would alter the form_tag helper to include
the id and it would be available during post. But thanks, your feedback
was very helpful and I am going to continue studying to better
understand rails architecture.
When you read about the form_for helper versus the form_tag helper, you
will see that the item ID will be a part of the parameters when you
receive the form contents at your controller for the update method,
without any additional items being required.
Walter
Sorry, I just come from .net and I am used to Request.Querystring[]
which is able to fetch any URL parameter on post. The Request object, in
this case, takes care storing the URL parameter values. One other idea
you just gave me is to create an input field which would obtain the
value from the URL. I mean it would alter the form_tag helper to include
the id and it would be available during post. But thanks, your feedback
was very helpful and I am going to continue studying to better
understand rails architecture.