【问题标题】:How to add a link_to in a select_tag with Rails/ERB?如何使用 Rails/ERB 在 select_tag 中添加 link_to?
【发布时间】:2013-01-05 08:43:21
【问题描述】:

我的 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)。

【问题讨论】:

  • 当您从选择标签中选择一个选项时,您希望将页面重定向到您想要的链接,是吗?
  • 是的@shweta,我会用这个信息更新我的问题。

标签: ruby-on-rails erb helpers 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>

【讨论】:

  • 感谢@Evgeniy,好像我无法在 select_tag 中添加 link_to。是 Jquery!
  • 这是我的解决方案,但我进行了更改。 window.location.href = 网址;如果您使用替换,后退按钮将无法正常工作,但如果您将 href 设置为 url,它将;)
【解决方案2】:

试试:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 2019-03-30
    相关资源
    最近更新 更多