Forum: Ruby on Rails Passing multiple parameters within a form

4d970cd64b30776abd944168c75d8223?d=identicon&s=25 Richard (Guest)
on 2007-06-23 00:10
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?
21f7ed21f11a809050594c82eab11d67?d=identicon&s=25 Robert Walker (Guest)
on 2007-06-23 03:24
(Received via mailing list)
<% form_tag do %>
<p>
  <label for="name">Name:</label>
  <%= text_field_tag :name, params[:name] %>
</p>

<p>
  <label for="password">Password:</label>
  <%= password_field_tag :password, params[:password] %>
</p>
<p><%= submit_tag "Login" %></p>
<% end %>

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

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

Also notice that you use <% form_tag ... %> NOT <%= form_tag... %>.
No equal sign.  You end the form with <% end %>.
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2007-06-23 06:50
(Received via mailing list)
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
4d970cd64b30776abd944168c75d8223?d=identicon&s=25 Richard (Guest)
on 2007-06-23 11:58
Bill Walton 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.
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2007-06-23 16:52
(Received via mailing list)
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
4d970cd64b30776abd944168c75d8223?d=identicon&s=25 Richard (Guest)
on 2007-06-23 21:00
Bill Walton 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.
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2007-06-24 04:03
(Received via mailing list)
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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.