Rodrigo Lueneberg
2013-Jul-05 17:09 UTC
URL "id" not part of the params objects after post
Why is the URL "id" element not part of the params objects after submitting a form via form_tag? Steps: 1. Go to URL http://localhost:3000/users/delete?id=45 2. Press Submit button 3. Read id param in order to delete user, then redirect form <p>Delete Confirmation</p> <%= 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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/091b1ca83a69eed98630de550e88d2a0%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Robert Walker
2013-Jul-05 17:27 UTC
Re: URL "id" not part of the params objects after post
Rodrigo Lueneberg 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/fe271ca6db3b1722518ebea78907023b%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Rodrigo Lueneberg
2013-Jul-05 17:39 UTC
Re: URL "id" not part of the params objects after post
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. 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. I guess I will just keep reading more about rails. Rod -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/65d0d0b666406921ae5f23adefeeae82%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Robert Walker
2013-Jul-05 19:30 UTC
Re: URL "id" not part of the params objects after post
Rodrigo Lueneberg 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'') # => <form action="/posts" method="post"> 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). -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/072527dac68e973ab16f0e670d0d7862%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Rodrigo Lueneberg
2013-Jul-05 23:03 UTC
Re: URL "id" not part of the params objects after post
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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6a8e4f4368f63f5d90888ac6099f5591%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Walter Lee Davis
2013-Jul-06 00:09 UTC
Re: URL "id" not part of the params objects after post
On Jul 5, 2013, at 7:03 PM, Rodrigo Lueneberg 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> > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6a8e4f4368f63f5d90888ac6099f5591%40ruby-forum.com. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/90D9CFB0-2F78-4EE3-A960-83B6B9CDF317%40wdstudio.com. For more options, visit https://groups.google.com/groups/opt_out.