2013-01-22 22 views
6

Ho il seguente codice nel mio index.html.erbCome aggiungere un link a un tag select con Rails/ERB?

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

Dove vorrei aggiungere un aiutante link_to all'interno di quel blocco? Posso persino aggiungere un link a per select_tag?

Il link_to desiderato sarebbe andato a '/ Teamleader/ID_OF_OPTION_PICKED'

UPDATE:

Per essere più chiaro; quando un utente seleziona un'opzione dal tag select, voglio reindirizzare la pagina al link desiderato (da link_to).

+1

quando si seleziona un'opzione dal tag select, si desidera reindirizzare la pagina sul link desiderato, è vero? – shweta

+2

si @shweta, aggiornerò la mia domanda con queste informazioni. – asing

risposta

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> 
+2

Grazie a @Evgeniy, sembra che non possa aggiungere un link a un tag select. Jquery è! – asing

+0

Questa era la mia soluzione, ma ho apportato una modifica. window.location.href = url; Se si utilizza la sostituzione, il pulsante Indietro non funzionerà correttamente, ma se si imposta l'href sull'URL lo farà;) –

5

Prova:

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