Passing multiple parameters within a form

Hi Richard,

Richard wrote:

I'm trying to pass multiple parameters to the resulting
action called by a form.

I know you can do:

<%= form_tag( { :action => 'search', :id => params[:id]}, :method =>
"GET" ) %>

To pass the id value, but if I add any more values after this they are
ignored, e.g.

<%= form_tag( { :action => 'search', :id => params[:id], :letter =>
params[:letter]}, :method => "GET" ) %>

How do I pass multiple parameters in addition to the id?

You're misunderstanding the syntax. All the values of the fields in your form get passed in the params hash to the action automatically. Set yourself up a sandbox app and experiment to see for yourself. Use params.inspect in the action's view.


Hi Richard,

I'm not sure I'm following.

Richard wrote:

I have other parameters that are getting lost.
Let me give an example.

It'll be easier to assist you if you post some additional code, but I'll give it a shot with what you've given us so far.

It sounds like you've got two forms that get submitted, in sequence, to one or more controller methods. The first form submits two params that are used to retrieve a set of records that are displayed on a page with the second form. It sounds like you're wanting the second form to submit both the original two params and an additional one (so that the controller can retrieve the same record set that resulted from the first retrieval, but sorted one of two ways). It sounds like the problem you're having is in rendering the second form so that it will submit all three items as params. Is that right?

If so, the easy fix would be to render the first two items as hidden_field items on the second form. Then when the second form gets submitted, the method will have access to all three params.

If that's not helpful, you'll need to post some additional code for me to help. Or maybe someone else will be able to grok what's going on better than me and be able to provide better assistance with what you're already provided.

Best regards,

Hi Richard,

Richard wrote:

Thanks Bill.

You're very welcome.

You're pretty much spot on with what you've said. The
hidden field thing works fine but I wasn't sure whether to use it as it
puts a lot of stuff in the URL, which kinda looks ugly and maybe a bit
revealing of what the code is doing.

Not 100% sure in this case, but I think you can 'fix' the URL issues with routes.

Best regards,