Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send querystring params as part of form post

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.

like image 492
Mark Hoffman Avatar asked Feb 02 '10 16:02

Mark Hoffman


People also ask

Can we send query params in post request?

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.

Can I send query string in POST method?

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.

How do I pass Querystring?

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.


2 Answers

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.

like image 122
Mark Hoffman Avatar answered Oct 25 '22 07:10

Mark Hoffman


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 %>
like image 23
John Topley Avatar answered Oct 25 '22 06:10

John Topley