本文介绍了如何使用Rails / ERB在select_tag中添加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吗?
Where would I add a link_to helper within that block? Can I even add a link_to for select_tag?
所需的链接将转到'/ Teamleader / ID_OF_OPTION_PICKED'
The desired link_to would go to '/Teamleader/ID_OF_OPTION_PICKED'
更新:
更清晰;当用户从select标记中选择一个选项时,我想将页面重定向到所需的链接(从link_to)。
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).
推荐答案
<%= 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>
这篇关于如何使用Rails / ERB在select_tag中添加link_to?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!