开发者

Send querystring params as part of form post

开发者 https://www.devze.com 2022-12-19 02:35 出处:网络
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 g

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 po开发者_JS百科st, 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.


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 %>


<% form_tag params.merge(:action=>"someAction") do %>

- No route matches [POST]


Use hidden_field_tag if you're using a GET request.

In our case we were using a simple form with a select for setting the Per Page values for pagination. We found that any existing GET params were cleared when submitting this form. To fix this we used hidden_field_tags in our form.

Inside of your form, just set hidden_field_tags for the existing GET params, like so:

form_content = request.query_parameters.collect do |key, value|
  hidden_field_tag key, value
end

This will ensure that your existing params persist.

0

精彩评论

暂无评论...
验证码 换一张
取 消