Passing multiple parameters within a form

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?

<% form_tag do %>

Name: <%= text_field_tag :name, params[:name] %>

Password: <%= password_field_tag :password, params[:password] %>

<%= submit_tag "Login" %>

<% end %>

This sends params[:name] and params[:password] that you can use in
your controller.

Passing multiple parameters is what a form does. :slight_smile: You’re over-
thinking it.

Also notice that you use <% form_tag … %> NOT <%= form_tag… %>.
No equal sign. You end the form with <% end %>.

Hi Richard,

Richard wrote:

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.

hth,
Bill

Bill W. wrote:

Hi Richard,

Richard wrote:

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.

hth,
Bill

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

The user specifies that they want to list all restaurants that match a
particular cuisine and begin with a particular letter. The appropriate
page of results is then loaded with a select menu at the top of the page
which has two options - sort alphabetically, sort by rating (this is my
form). When I call this form however it is performing the sort on all of
the restaurants.

I can make it perform the sort on just the restaurants matching the
specific cuisine by using the code I posted above, but any further
parameters (such as the specific letter) are not carried over.

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,
Bill

Bill W. wrote:

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,
Bill

Thanks Bill. 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.

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,
Bill