如何添加的link_to在使用Rails / ERB一个select_tag?(How to add

2019-08-07 18:08发布

我有下面的代码在我的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”

更新:

为了更清晰; 当用户选择从选择标签的选项,我想在页面重定向到所需的链接(来自的link_to)。

Answer 1:

<%= 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>


Answer 2:

尝试:

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


文章来源: How to add a link_to in a select_tag with Rails/ERB?