Creating a select dropdown box with links to methods

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.

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.

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

Alex,

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