Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a link_to in a select_tag with Rails/ERB?

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

like image 628
asing Avatar asked Jan 22 '13 06:01

asing


2 Answers

<%= 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>
like image 117
Eugene Rourke Avatar answered Sep 19 '22 11:09

Eugene Rourke


Try:

<%= select_tag 'team_id', options_from_collection_for_select(@teams, "id", "name"),:onchange => "window.location.replace('/Teamleader/'+this.value);" %>
like image 30
shweta Avatar answered Sep 22 '22 11:09

shweta