<div class="nested-fields">
  <% if f.object && f.object.send(attribute) %>
    <% klass = f.object.send(attribute).class %>

    <div class="chip">
      <%= link_to f.object.send(attribute) do %>
        <span class="<%= klass.color %>-text">
          <i class="material-icons left"><%= klass.icon %></i>
        </span>
        <%= f.object.send(attribute).name %>
      <% end %>
    </div>

    <%= link_to_remove_association 'remove', f %>

  <% else %>
    <% uuid = SecureRandom.uuid %>
    <% klass = parent.send(attribute.pluralize).build.class.name.downcase %>

    <div class="input-field">
      <%= f.label attribute %>
      <%# todo scope to universe %>
      <%= f.select "#{attribute}_id", current_user.send(klass.pluralize).map { |c| [c.name, c.id] }, include_blank: true %>
      <%= link_to_remove_association 'remove', f %>
    </div>

    <script type="text/javascript">
      $(document).ready(function() {
        $('select').material_select();
      });
    </script>

  <% end %>
</div>
<div class="nested-fields">
  <% if f.object && f.object.send(attribute) %>
    <% klass = f.object.send(attribute).class %>

    <div class="chip">
      <%= link_to f.object.send(attribute) do %>
        <span class="<%= klass.color %>-text">
          <i class="material-icons left"><%= klass.icon %></i>
        </span>
        <%= f.object.send(attribute).name %>
      <% end %>
    </div>

    <%= link_to_remove_association 'remove', f %>

  <% else %>
    <% uuid = SecureRandom.uuid %>
    <% klass = parent.send(attribute.pluralize).build.class.name.downcase %>

    <div class="input-field">
      <%= f.label attribute %>
      <%# todo scope to universe %>
      <%= f.select "#{attribute}_id", current_user.send(klass.pluralize).sort_by(&:name).map { |c| [c.name, c.id] }.compact, include_blank: true %>
      <%= link_to_remove_association 'remove', f %>
    </div>

    <script type="text/javascript">
      $(document).ready(function() {
        $('select').material_select();
      });
    </script>

  <% end %>
</div>
Sort link dropdowns by name
Sort link dropdowns by name
