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

烂漫一生 提交于 2019-12-10 03:19:03

问题


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


回答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>



回答2:


Try:

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


来源:https://stackoverflow.com/questions/14453136/how-to-add-a-link-to-in-a-select-tag-with-rails-erb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!