# Creating a select dropdown box with links to methods

**URL:** <https://www.ruby-forum.com/t/creating-a-select-dropdown-box-with-links-to-methods/58541>\
**Category:** Rails\
**Created:** [April 22, 2006, 10:27pm UTC](https://www.ruby-forum.com/t/creating-a-select-dropdown-box-with-links-to-methods/58541 "2006-04-22T22:27:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marstoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@marstoni](https://www.ruby-forum.com/u/marstoni)\
**Post date:** [April 22, 2006, 10:27pm UTC](https://www.ruby-forum.com/t/creating-a-select-dropdown-box-with-links-to-methods/58541/1 "2006-04-22T22:27:33Z")

</div>

What would be the best way to create a form select dropdown box and upon  
“submit” have it directly go to a controller method view?

I have something that somewhat works, but I think there is probably a  
much easier way to do it. As of now, I have the dropdown list which  
when the form is submitted is handed to a controller method which  
basically

I have my dropdown box:

\<%= form\_tag :action =\> ‘select\_view’ %\>  
**Select a View:**  
  
Choice 1  
Choice 2  
  
\<%= submit\_tag(“View”) %\>  
\<%= end\_form\_tag %\>

Then I have a methond in my controller that based on the selct box  
selection, redirects to a different method/view:

def select\_view  
# Depending on the selection, redirect to a certain view.  
if params[:selection] = ‘choice1’  
redirect\_to(:action =\> ‘method1’)  
elsif params[:selection] = ‘choice2’  
redirect\_to(:action =\> ‘method2’)  
else  
redirect\_to(:action =\> ‘method3’)  
end  
end

I’m sure there is a better way to do this but I can’t find it. Plus,  
even though they are quite trivial I’m sure my if/elsif statement is  
messed up somewhere as it works, but always redirects to the first if  
parameter if another choice in the select list is chosen. Thanks.

---

<div class="post-metadata">

**Author:** ![marstoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@marstoni](https://www.ruby-forum.com/u/marstoni)\
**Post date:** [April 22, 2006, 11:11pm UTC](https://www.ruby-forum.com/t/creating-a-select-dropdown-box-with-links-to-methods/58541/2 "2006-04-22T23:11:37Z")

</div>

Marston A. wrote:

> What would be the best way to create a form select dropdown box and upon  
> “submit” have it directly go to a controller method view?
> 
> I have something that somewhat works, but I think there is probably a  
> much easier way to do it. As of now, I have the dropdown list which  
> when the form is submitted is handed to a controller method which  
> basically
> 
> I have my dropdown box:
> 
> \<%= form\_tag :action =\> ‘select\_view’ %\>  
> **Select a View:**
> 
> Choice 1 Choice 2 \<%= submit\_tag("View") %\> \<%= end\_form\_tag %\>
> 
> Then I have a methond in my controller that based on the selct box  
> selection, redirects to a different method/view:
> 
> def select\_view  
> # Depending on the selection, redirect to a certain view.  
> if params[:selection] = ‘choice1’  
> redirect\_to(:action =\> ‘method1’)  
> elsif params[:selection] = ‘choice2’  
> redirect\_to(:action =\> ‘method2’)  
> else  
> redirect\_to(:action =\> ‘method3’)  
> end  
> end
> 
> I’m sure there is a better way to do this but I can’t find it. Plus,  
> even though they are quite trivial I’m sure my if/elsif statement is  
> messed up somewhere as it works, but always redirects to the first if  
> parameter if another choice in the select list is chosen. Thanks.

First of all, “=” is the assignment operator and “==” is the comparison  
operator. So try:

def select\_view  
# Depending on the selection, redirect to a certain view.  
if params[:selection] == ‘choice1’  
redirect\_to(:action =\> ‘method1’)  
elsif params[:selection] == ‘choice2’  
redirect\_to(:action =\> ‘method2’)  
else  
redirect\_to(:action =\> ‘method3’)  
end  
end

But heres what i would do:

#controller  
class SomeController \< ApplicationController  
SELECTABLE\_ACTIONS = [:show, :show\_log, :happy\_dance]

def select\_view\_form  
@select\_options = SomeController::SELECTABLE\_ACTIONS.map do |action|  
[action.to\_s.humanize, action]  
end  
end

def select\_view  
if SELECTABLE\_ACTIONS.include? params[:selection].to\_sym  
redirect\_to :action =\> params[:selection]  
else  
redirect\_to :action =\> ‘default\_action’  
end  
end  
end

#view  
\<%= form\_tag :action =\> ‘select\_view’ %\>  
**Select a View:**  
  
\<%= options\_for\_select @select\_options %\>  
  
\<%= submit\_tag(“View”) %\>  
\<%= end\_form\_tag %\>

SELECTABLE\_ACTIONS contains a sybmol for each action we want to supply a  
link to.

We use a helper method to generate some option tags from our controller  
constant. Note you have to use :: notation to access a non-local  
constant. It should generate:

 Show Show log Happy dance

In the select\_view action, we see if the provided option is in our list  
of selectable action, and if it is, redirect there, wherever that may  
be. If it’s not in the list, then we redirect to our default action.

Once it’s all setup, just add action names to the SELECTABLE\_ACTIONS  
array to enable people to link to them. It keeps the control of this  
functionality in a single line of setup.

---

<div class="post-metadata">

**Author:** ![marstoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@marstoni](https://www.ruby-forum.com/u/marstoni)\
**Post date:** [April 22, 2006, 11:12pm UTC](https://www.ruby-forum.com/t/creating-a-select-dropdown-box-with-links-to-methods/58541/3 "2006-04-22T23:12:40Z")

</div>

oops, that code sample was supposed to be:

#controller  
class SomeController \< ApplicationController  
SELECTABLE\_ACTIONS = [:show, :show\_log, :happy\_dance]

def select\_view  
if SELECTABLE\_ACTIONS.include? params[:selection].to\_sym  
redirect\_to :action =\> params[:selection]  
else  
redirect\_to :action =\> ‘default\_action’  
end  
end  
end

#SomeController Helper  
def select\_view\_options\_tags  
options = SomeController::SELECTABLE\_ACTIONS.map do |action|  
[action.to\_s.humanize, action]  
end  
options\_for\_select options  
end

#view  
\<%= form\_tag :action =\> ‘select\_view’ %\>  
**Select a View:**  
  
\<%= select\_view\_options\_tags %\>  
  
\<%= submit\_tag(“View”) %\>  
\<%= end\_form\_tag %\>

---

<div class="post-metadata">

**Author:** ![marstoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@marstoni](https://www.ruby-forum.com/u/marstoni)\
**Post date:** [April 22, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/creating-a-select-dropdown-box-with-links-to-methods/58541/4 "2006-04-22T23:46:30Z")

</div>

Alex,

Thanks for the great info, I’ll give it a shot.
