简体   繁体   English

如何在Rails中显示html options_from_collection_for_select

[英]How to show html in rails options_from_collection_for_select

I used rails options_from_collection_for_select for select. 我使用rails options_from_collection_for_select进行选择。

options_from_collection_for_select(@tenants, :id, :tenant_text_value, current_tenant.try(:id))

def tenant_text_value
  "<a href='http://www.google.com'>google</a>".html_safe
end

I want <option value="215"><a href='http://www.google.com'>google</option> and when I click a I go to Google. 我需要<option value="215"><a href='http://www.google.com'>google</option> ,当我单击a我转到Google。

However, I just get <option value="215">Google</option> . 但是,我只是得到<option value="215">Google</option>

If I use "<a href='http://www.google.com'>google</a>".to_s , I get <option value="215"><a href='http://www.google.com'>google</a></option> . 如果我使用"<a href='http://www.google.com'>google</a>".to_s"<a href='http://www.google.com'>google</a>".to_s得到<option value="215"><a href='http://www.google.com'>google</a></option> Just a text, not a link. 只是文本,而不是链接。

What should I do ? 我该怎么办 ?

I think I know what you want to do and you have a flawed on what you want to do. 我想我知道您想做什么,而您想做什么却有缺陷。

I think that you want to do is when someone selects something to go to a page 我认为您要做的是当某人选择要转到页面的内容时

Expected output 预期产量

<select id="abc">
    <option value="http://www.google.com">Google</option>
    <option value="http://www.yahoo.com">Yahoo</option>
    <option value="http://www.google.com">Bing</option>
</select>

$("#abc").change(function() {
    window.location.href = $(this).val();
});

Rails 滑轨

<%= 
  select_tag "abc", 
  options_from_collection_for_select(@tenants, @tenant.url, @tenant.name", @tenant.id) 
%>

I hope that this helps you out 我希望这可以帮助您

Happy Hacking 快乐黑客

<%= select_tag '<some_id>', options_from_collection_for_select(@tenants, :id, :tenant_text_value, current_tenant.try(:id)),:onchange => "window.location.href(this.value);" %>

or 要么

<%= select_tag '<some_id>', options_from_collection_for_select(@tenants, :id, :tenant_text_value, current_tenant.try(:id)) %>

<script>
    $(function(){
      $('#team_id').bind('change', function () {
         var url = $(this).val()
          if (url) {
              window.location.href(url);
          }
          return false;
      });
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM