# Passing Parameter from View to Controller

**URL:** <https://www.ruby-forum.com/t/passing-parameter-from-view-to-controller/141896>\
**Category:** Rails\
**Created:** [July 1, 2008, 2:59pm UTC](https://www.ruby-forum.com/t/passing-parameter-from-view-to-controller/141896 "2008-07-01T14:59:10Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![McLobster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df788c/32.png) [@McLobster](https://www.ruby-forum.com/u/McLobster)\
**Post date:** [July 1, 2008, 2:59pm UTC](https://www.ruby-forum.com/t/passing-parameter-from-view-to-controller/141896/1 "2008-07-01T14:59:10Z")

</div>

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

  
  
Choose Filter Type  
\<% @filter\_lists.each do |filterlist| %\>  
\<option value="\<%= filterlist.id %\>"  
\<%= ’ selected’ if filterlist.id == @filter\_lists.id  
%\>\>  
\<%= filterlist.name %\>  
  
\<% end %\>  

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

---

<div class="post-metadata">

**Author:** ![McLobster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df788c/32.png) [@McLobster](https://www.ruby-forum.com/u/McLobster)\
**Post date:** [July 1, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/passing-parameter-from-view-to-controller/141896/2 "2008-07-01T15:01:31Z")

</div>

On 1 Jul 2008, at 13:58, McLobster wrote:

> 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

---

<div class="post-metadata">

**Author:** ![McLobster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df788c/32.png) [@McLobster](https://www.ruby-forum.com/u/McLobster)\
**Post date:** [July 1, 2008, 3:08pm UTC](https://www.ruby-forum.com/t/passing-parameter-from-view-to-controller/141896/3 "2008-07-01T15:08:35Z")

</div>

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

Then everything below would fall into my :search\_options form?

---

<div class="post-metadata">

**Author:** ![McLobster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df788c/32.png) [@McLobster](https://www.ruby-forum.com/u/McLobster)\
**Post date:** [July 1, 2008, 3:17pm UTC](https://www.ruby-forum.com/t/passing-parameter-from-view-to-controller/141896/4 "2008-07-01T15:17:59Z")

</div>

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!”)%\>

  
  
Choose Filter Type  
\<% @filter\_lists.each do |filterlist| %\>  
\<option value="\<%= filterlist.id %\>"  
\<%= ’ selected’ if filterlist.id == @filter\_lists.id  
%\>\>  
\<%= filterlist.name %\>  
  
\<% end %\>  

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

  
  
Choose Filter Type  
\<% @filter\_lists.each do |filterlist| %\>  
\<option value="\<%= filterlist.id %\>"  
\<%= ’ selected’ if filterlist.id == @filter\_lists.id  
%\>\>  
\<%= filterlist.name %\>  
  
\<% end %\>  

```
  <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.

---

<div class="post-metadata">

**Author:** ![McLobster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df788c/32.png) [@McLobster](https://www.ruby-forum.com/u/McLobster)\
**Post date:** [July 1, 2008, 3:12pm UTC](https://www.ruby-forum.com/t/passing-parameter-from-view-to-controller/141896/5 "2008-07-01T15:12:08Z")

</div>

On 1 Jul 2008, at 14:07, McLobster wrote:

> So… do I get rid of the second form declaration:
> 
> 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

---

<div class="post-metadata">

**Author:** ![McLobster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df788c/32.png) [@McLobster](https://www.ruby-forum.com/u/McLobster)\
**Post date:** [July 1, 2008, 3:32pm UTC](https://www.ruby-forum.com/t/passing-parameter-from-view-to-controller/141896/6 "2008-07-01T15:32:38Z")

</div>

On 1 Jul 2008, at 14:17, McLobster wrote:

> ```
> <option value="<%= filterlist.id %>"
> 
> <% @filter_lists.each do |filterlist| %>
> 
> ```
> 
> 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

 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

---

<div class="post-metadata">

**Author:** ![McLobster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df788c/32.png) [@McLobster](https://www.ruby-forum.com/u/McLobster)\
**Post date:** [July 1, 2008, 3:46pm UTC](https://www.ruby-forum.com/t/passing-parameter-from-view-to-controller/141896/7 "2008-07-01T15:46:44Z")

</div>

On 1 Jul 2008, at 14:39, McLobster wrote:

> 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

 tag inside a form like that. A td can only  
be contained within a tr.

Fred

---

<div class="post-metadata">

**Author:** ![McLobster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df788c/32.png) [@McLobster](https://www.ruby-forum.com/u/McLobster)\
**Post date:** [July 1, 2008, 3:40pm UTC](https://www.ruby-forum.com/t/passing-parameter-from-view-to-controller/141896/8 "2008-07-01T15:40:17Z")

</div>

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”\>

  
  
Choose Filter Type  
\<% @filter\_lists.each do |filterlist| %\>  
\<option value="\<%= filterlist.id %\>"  
\<%= ’ selected’ if filterlist.id == @filter\_lists.id  
%\>\>  
\<%= filterlist.name %\>  
  
  
\<% end %\>  

```
  <input style="font-size: 12pt; font-weight:bold" type="submit"

```

value=“Filter” /\>

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

---

<div class="post-metadata">

**Author:** ![McLobster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df788c/32.png) [@McLobster](https://www.ruby-forum.com/u/McLobster)\
**Post date:** [July 1, 2008, 3:57pm UTC](https://www.ruby-forum.com/t/passing-parameter-from-view-to-controller/141896/9 "2008-07-01T15:57:20Z")

</div>

The

 and 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.

---

<div class="post-metadata">

**Author:** ![McLobster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df788c/32.png) [@McLobster](https://www.ruby-forum.com/u/McLobster)\
**Post date:** [July 1, 2008, 4:24pm UTC](https://www.ruby-forum.com/t/passing-parameter-from-view-to-controller/141896/10 "2008-07-01T16:24:57Z")

</div>

On 1 Jul 2008, at 14:56, McLobster wrote:

> The
> 
> and 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

---

<div class="post-metadata">

**Author:** ![McLobster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df788c/32.png) [@McLobster](https://www.ruby-forum.com/u/McLobster)\
**Post date:** [July 1, 2008, 4:28pm UTC](https://www.ruby-forum.com/t/passing-parameter-from-view-to-controller/141896/11 "2008-07-01T16:28:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![McLobster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df788c/32.png) [@McLobster](https://www.ruby-forum.com/u/McLobster)\
**Post date:** [July 1, 2008, 4:08pm UTC](https://www.ruby-forum.com/t/passing-parameter-from-view-to-controller/141896/12 "2008-07-01T16:08:31Z")

</div>

On Tue, Jul 1, 2008 at 6:56 AM, McLobster [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The and aren’t contained in the fragment I pasted, but  
> they’re there, don’t worry. This code works, I’ve tested it.

“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:

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

Hassan S. ------------------------ removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![McLobster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df788c/32.png) [@McLobster](https://www.ruby-forum.com/u/McLobster)\
**Post date:** [July 1, 2008, 4:32pm UTC](https://www.ruby-forum.com/t/passing-parameter-from-view-to-controller/141896/13 "2008-07-01T16:32:35Z")

</div>

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.
