Is there a way to capture the querystring and send that along as part of a form post? I'm using Rails 2.3.5 and my user is on a page that has multiple querystring parameters. On this page, they are going to submit a form. Inside the action that receives the post, I want to know what those querystring parameters were. Obviously, they are not sent as part of the post. So I need the actual form values, plus the querystring params that were on the page when the user submitted the form.
I'm sure I could write some nasty javascript that would shove the querystring params into hidden fields on the form so they would be available, but that seems ugly. My Googling hasn't turned up much, which makes me wonder if I'm just going about this all wrong. To make matters worse, I'm a Rails newbie.
Appreciate any pointers or ideas to get me going in the right direction.
POST should not have query param. You can implement the service to honor the query param, but this is against REST spec. "Why do you pass it in a query param?" Because, like for GET requests, the query parameters are used to refer to an existing resource.
Post uses the message body to send the information back to the server, as opposed to Get, which uses the query string (everything after the question mark). It is possible to send both a Get query string and a Post message body in the same request, but that can get a bit confusing so is best avoided.
To pass in parameter values, simply append them to the query string at the end of the base URL. In the above example, the view parameter script name is viewParameter1.
A friend of mine showed me what I believe is an easier way:
<% form_tag params.merge(:action=>"someAction") do %>
Merging params into the hash necessary for making the form_tag did the trick perfectly.
The preferred way would be to use hidden fields. I haven't tried it, but I think you can specify additional query string parameters within the *_path
or *_url
helpers. Something like:
<% form_for(@post,
:url => post_path(@post, :foo => 'foo', :bar => 'bar')) do |f| %>
...
<% end %>
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