如何在 rails options_from_collection_for_select 中显示 html

How to show html in rails options_from_collection_for_select

我用 rails options_from_collection_for_select 代替 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

我想要 <option value="215"><a href='http://www.google.com'>google</option>,当我单击 a 时,我会转到 Google。

然而,我只得到<option value="215">Google</option>

如果我使用 "<a href='http://www.google.com'>google</a>".to_s,我会得到 <option value="215"><a href='http://www.google.com'>google</a></option>。只是文本,不是 link.

我该怎么办?

我想我知道你想做什么,而且你对你想做的事情有缺陷。

我想你想要做的是当有人选择一些东西去页面时

预期输出

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

希望对您有所帮助

快乐黑客

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

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