2013-01-22 32 views
6

我在下面的代碼index.html.erb如何使用Rails/ERB在select_tag中添加link_to?

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

我會在哪裏該塊內添加的link_to助手?我甚至可以爲select_tag添加link_to?

所需的link_to會去 '/ Teamleader/ID_OF_OPTION_PICKED'

UPDATE:

更清晰;當用戶從選擇標籤中選擇一個選項時,我想將頁面重定向到所需的鏈接(從link_to)。

+1

當你從選擇標籤,你要的頁面重定向到您所需的鏈接,它是一個選項? – shweta

+2

是@shweta,我會用這個信息更新我的問題。 – asing

回答

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

謝謝@Evgeniy,好像我無法在select_tag中添加link_to。 Jquery是! – asing

+0

這是我的解決方案,但我做了一個改動。 window.location.href = url; 如果您使用替換,後退按鈕將無法正常工作,但如果您將href設置爲它的URL;) –

5

嘗試:

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