8

I have the following code in my index.html.erb

 <%= select_tag 'team_id', options_for_select(@teams.map{|team| ["#{team.name} #
  {team.nick}", team.id] }) %>

Where would I add a link_to helper within that block? Can I even add a link_to for select_tag?

The desired link_to would go to '/Teamleader/ID_OF_OPTION_PICKED'

UPDATE:

To be more clear; when a user selects an option from the select tag, I want to redirect the page to the desired link (from link_to).

asing
  • 385
  • 2
  • 7
  • 16

2 Answers2

6
<%= select_tag 'team_id', options_from_collection_for_select(@teams, "id", "name") %>

<script>
    $(function(){
      $('#team_id').bind('change', function () {
         var url = "/Teamleader/" + $(this).val()
          if (url) {
              window.location.replace(url);
          }
          return false;
      });
    });
</script>
Eugene Rourke
  • 4,934
  • 1
  • 22
  • 24
  • 2
    Thanks @Evgeniy, seems like I can't add a link_to in a select_tag. Jquery it is! – asing Jan 22 '13 at 07:16
  • This was my solution but I made a change. window.location.href = url; If you use replace, the back button will not work correctly, but if you set the href to the url it will ;) – Bob Roberts Jan 05 '18 at 22:08
6

Try:

<%= select_tag 'team_id', options_from_collection_for_select(@teams, "id", "name"),:onchange => "window.location.replace('/Teamleader/'+this.value);" %>
shweta
  • 8,019
  • 1
  • 40
  • 43