<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.text_color %>">
          <i class="material-icons left"><%= klass.icon %></i>
        </span>
        <%= f.object.send(attribute).name %>
      <% end %>
    </div>
    <%= link_to_remove_association f do %>
      <i class="material-icons red-text" style="position: relative; top: 8px; left: -3px;">close</i>
    <% end %>

  <% else %>
    <% klass = parent.send(attribute.pluralize).klass.name.downcase %>

    <div class="input-field">
      <%= f.label attribute, class: 'active' %>

      <div class="row">
        <div class="col s10">
          <%=
            f.select "#{attribute}_id",
              @linkables_cache[klass.titleize] || [],
              include_blank: true
          %>
        </div>

        <div class="col s2">
          <%= link_to_remove_association f do %>
            <i class="material-icons red-text" style="position: relative; top: 10px; left: -15px;">close</i>
          <% end %>
        </div>
      </div>
    </div>

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

  <% end %>
</div>
