Passing Parameter from View to Controller

Hey there!

I have a dropdown list ready to go on my rails app. It calls my
action. My action works. Everythings good. Except I can't figure out
for the life of me how to pass the value selected in the dropdown list
as a parameter to the action. I've discovered that forms are involved,
though no matter how many example I go through I can't get them to
work. Here's what I've got:

The dropdown list in the view:
<% form_tag({:controller => 'remote_site_controller',
              :action => 'filter'}) do %>
    <%= text_field_tag(:search_options, params[:search_options])%>
    <%= submit_tag("Display text!")%>
  <% end -%>
    <form id="filter_form" name="filter_form" action="filter"
method="POST">
      <td class="option">
           <select name="filterlist[name]">
             <option value="">Choose Filter Type
              <% @filter_lists.each do |filterlist| %>
              <option value="<%= filterlist.id %>"
                    <%= ' selected' if filterlist.id == @filter_lists.id
%>>
                    <%= filterlist.name %>
                </option>
                <% end %>
            </select>

      <input style="font-size: 12pt; font-weight:bold" type="submit"
value="Filter" />

        </td>
          </form>

And here's my action:

def filter
    type = params[:search_options]
    render_text type
end

I've commented everything out of my action at the moment, and threw in
the render_text just to see what comes up. So far, for every attempt
I've made, render_text gives me nothing (leaving me to assume that
type is empty and not receiving the parameter). If you couldn't tell,
I've never used a form before so I'm running around in the dark, for
the most part. If anyone could drop me a hint I'd greatly appreciate
it, thanks.

Brad

Hey there!

I have a dropdown list ready to go on my rails app. It calls my
action. My action works. Everythings good. Except I can't figure out
for the life of me how to pass the value selected in the dropdown list
as a parameter to the action. I've discovered that forms are involved,
though no matter how many example I go through I can't get them to
work. Here's what I've got:

You've got two forms and your select is not in the form that you're
submitting (rails has helpers for creating select tags by the way)

Fred

So... do I get rid of the second form declaration:

<form id="filter_form" name="filter_form" action="filter"
method="POST">

Then everything below would fall into my :search_options form?

So... do I get rid of the second form declaration:

<form id="filter_form" name="filter_form" action="filter"
method="POST">

Then everything below would fall into my :search_options form?

You could leave it if you want, it doesn't matter. the important thing
is that any input you want submitted should be contained in the first
form (and that includes the submit button, or else it will still be
submitting the second form)

Fred

I think I've almost got it. So I rearranged everything like this...

  <% form_tag({:action => 'filter'}) do %>
    <%= text_field_tag(:search_options, params[:search_options])%>
    <%= submit_tag("Display text!")%>
            <td class="option">
           <select name="filterlist[name]">
             <option value="">Choose Filter Type
              <% @filter_lists.each do |filterlist| %>
              <option value="<%= filterlist.id %>"
                    <%= ' selected' if filterlist.id == @filter_lists.id
%>>
                    <%= filterlist.name %>
                </option>
                <% end %>
            </select>

      <input style="font-size: 12pt; font-weight:bold" type="submit"
value="Filter" />
  <% end -%>

But the dropdown didn't show up. So instead I put everything outside
the <% end -%>:

  <% form_tag({:action => 'filter'}) do %>
    <%= text_field_tag(:search_options, params[:search_options])%>
    <%= submit_tag("Display text!")%>
  <% end -%>
            <td class="option">
           <select name="filterlist[name]">
             <option value="">Choose Filter Type
              <% @filter_lists.each do |filterlist| %>
              <option value="<%= filterlist.id %>"
                    <%= ' selected' if filterlist.id == @filter_lists.id
%>>
                    <%= filterlist.name %>
                </option>
                <% end %>
            </select>

      <input style="font-size: 12pt; font-weight:bold" type="submit"
value="Filter" />

And when I click the button, nothing happens. So somehow my action
isn't being called... Or the button/dropdown isn't included in the
form.

I think I've almost got it. So I rearranged everything like this...

  <% form_tag({:action => 'filter'}) do %>
    <%= text_field_tag(:search_options, params[:search_options])%>
    <%= submit_tag("Display text!")%>
            <td class="option">
           <select name="filterlist[name]">
             <option value="">Choose Filter Type
              <% @filter_lists.each do |filterlist| %>
              <option value="<%= filterlist.id %>"
                   <%= ' selected' if filterlist.id == @filter_lists.id
%>>
                   <%= filterlist.name %>
               </option>
               <% end %>
           </select>

      <input style="font-size: 12pt; font-weight:bold" type="submit"
value="Filter" />
  <% end -%>

But the dropdown didn't show up. So instead I put everything outside
the <% end -%>:

  <% form_tag({:action => 'filter'}) do %>
    <%= text_field_tag(:search_options, params[:search_options])%>
    <%= submit_tag("Display text!")%>
  <% end -%>
            <td class="option">
           <select name="filterlist[name]">
             <option value="">Choose Filter Type
              <% @filter_lists.each do |filterlist| %>
              <option value="<%= filterlist.id %>"
                   <%= ' selected' if filterlist.id == @filter_lists.id
%>>
                   <%= filterlist.name %>
               </option>
               <% end %>
           </select>

      <input style="font-size: 12pt; font-weight:bold" type="submit"
value="Filter" />

And when I click the button, nothing happens. So somehow my action
isn't being called... Or the button/dropdown isn't included in the
form.

The button is outside the form (assuming you're talking about the
Filter button rather than the 'Display text' one), therefore clicking
it does not submit the form. It probably doesn't help that your html
is completely malformed (which is probably why the first version
didn't work). In no particular orcer you've got a <td> which you never
close (which sort of implies that the form above is at the top level
of the form, which is also not allowed). You don't close your option
tags. The w3c html validator should be able to help you there (and I'd
encourage you to read up on at least some basic html as you seem to be
flailing around in the dark). You might save yourself some grief by
using the rails helpers to generate your select box.

Fred

Gross. Alright, last try... is there anyway to pass the parameter (the
selected value in the dropdown list) to the controller just by using
this form? :

      <form id="filter_form" name="filter_form" action="filter"
method="POST">
            <td class="option">
           <select name="filterlist[name]">
             <option value="">Choose Filter Type
              <% @filter_lists.each do |filterlist| %>
              <option value="<%= filterlist.id %>"
                    <%= ' selected' if filterlist.id == @filter_lists.id
%>>
                    <%= filterlist.name %>
          </option>
                </option>
                <% end %>
            </select>

      <input style="font-size: 12pt; font-weight:bold" type="submit"
value="Filter" />
       </td>
  </form>

If not I'll start googling on how to do the select box with the rails
helpers.

Gross. Alright, last try... is there anyway to pass the parameter (the
selected value in the dropdown list) to the controller just by using
this form? :

You've still got syntactically invalid stuff there (and depending
where this file lives, action="filter" might be trying to call the
filter action on a different controller).
You also can't have a <td> tag inside a form like that. A td can only
be contained within a tr.

Fred

The <tr> and </tr> aren't contained in the fragment I pasted, but
they're there, don't worry. This code works, I've tested it. I just
don't know if its possible to pass a parameter with it. In the
controller I've tried

@type = params[:filter_form] and
@type = params["filter_form"]

but neither works. I've tested enough to know the action is being
called. Every example I've seen (containing forms) passes a parameter
with a rails form, which leads me to believe I need to use one, and
not the html form I've pasted above, to pass the value.

"tested" how? You've already said it doesn't work. When it passes
the W3C validator, it "works". Yours won't.

Most egregiously, your option tags are *nested* which is totally wrong.

And this: @type = params["filter_form"]

:: doesn't remotely match the parameter you've named here:

  <select name="filterlist[name]">

:: so I'm not sure what you'd expect to happen.

BTW, if you change your form method to "GET" you can see the
parameters in your browser's address bar -- might speed things up...

FWIW,

The <tr> and </tr> aren't contained in the fragment I pasted, but
they're there, don't worry. This code works, I've tested it. I just
don't know if its possible to pass a parameter with it. In the
controller I've tried

You've not understood me: a td tag's parent be contained a tr tag.
Your's is a form tag

@type = params[:filter_form] and
@type = params["filter_form"]

but neither works. I've tested enough to know the action is being
called. Every example I've seen (containing forms) passes a parameter
with a rails form, which leads me to believe I need to use one, and
not the html form I've pasted above, to pass the value.

rails forms and html forms are the same thing. the rails form helpers
just generate regular form tags.

the parameter would be available as params[:filterlist][:name] if it
were passed, but like I said the html is so mangled the browser could
be forgiven for not going it.
You can see all the parameters passed in development.log (or stick a
breakpoint in your action) rather than trying to guess what might be
there.

Fred

The webpage works fine. I host is locally and open it in a web
browser. Presto, it pops up, dropdown, button and all. The only thing
that doesn't work is passing the parameter.

So if I have <select name="filterlist[name]">
then the name of the parameter is filterlist[name]?
So then in my controller I should do something like:
@type = params["filterlist[name]"]

(I was using "filter_form" because thats the id of the form. Got to
identify it somehow, I thought that was how you did it)

Thanks for the tip with the "GET" method. Tried that, the correct
parameter showed up in the address bar, so I know its there. Just need
to nab it.

Eureeka!
params[:filterlist][:name] worked like a charm.
Thanks for sticking through it with me guys. And thanks for putting up
with me and my horrible html skills. I think I'm going to go throw a
party.