Forum: Ruby on Rails Creating a select dropdown box with links to methods

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Marston A. (Guest)
on 2006-04-23 00:27
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' %>
<strong>Select a View:</strong>
<select id="selection" name="selection">
	<option value="choice1">Choice 1</option>
	<option value="choice2">Choice 2</option>
</select>
<%= 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.
Alex W. (Guest)
on 2006-04-23 01:11
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' %>
> <strong>Select a View:</strong>
> <select id="selection" name="selection">
> 	<option value="choice1">Choice 1</option>
> 	<option value="choice2">Choice 2</option>
> </select>
> <%= 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' %>
  <strong>Select a View:</strong>
  <select id="selection" name="selection">
    <%= options_for_select @select_options %>
  </select>
  <%= 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:

  <option value="show">Show</option>
  <option value="show_log">Show log</option>
  <option value="happy_dance">Happy dance</option>

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.
Alex W. (Guest)
on 2006-04-23 01:12
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' %>
  <strong>Select a View:</strong>
  <select id="selection" name="selection">
    <%= select_view_options_tags %>
  </select>
  <%= submit_tag("View") %>
<%= end_form_tag %>
Marston A. (Guest)
on 2006-04-23 01:46
Alex,

Thanks for the great info, I'll give it a shot.
This topic is locked and can not be replied to.