如何在带有Rails / ERB的select_tag中添加link_to?

时间:2013-01-22 06:55:43

标签: ruby-on-rails erb helpers link-to

我的 index.html.erb

中包含以下代码
 <%= 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'

更新

更清楚;当用户从select标签中选择一个选项时,我想将页面重定向到所需的链接(来自link_to)。

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:6)

尝试:

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