upmin / upmin-admin-ruby

Framework for creating powerful admin backends with minimal effort in Ruby on Rails.
MIT License
757 stars 66 forks source link

how to add a dropdown for belongs_to #167

Closed navjeetc closed 9 years ago

navjeetc commented 9 years ago

I have a state model and a county model (county belongs to state). For creating/editing county model, how can I show a dropdown of all states in view so that I do not have to specify state_id?

juanjzv commented 9 years ago

That's the same thing I was trying to do but I can't figure out how. I hope the upmin team will help us.

jdurand commented 9 years ago

You can by creating a view partial. Lets say you're editing a Child that belongs_to :parent :

# views/upmin/partials/attributes/_child_parent_id.html.haml

.form-group{class: attribute.errors? ? "has-error" : ""}
  %label{for: attribute.form_id}
    = 'Parent'

  - if attribute.editable? && f = form_builder
    .enum
      = f.collection_select :parent_id, Parent.order(:name), :id, :name, include_blank: true

  - else
    %p.well
      = attribute.value

This is not very well documented but Upmin Admin guesses possible template names for each attributes based on their name, type and their model.

It tries to find a template file going from more to less precise.

To ease this pain I overrided the up_render method in Upmin::Railties::Render and added the following to see the possible view partials outputted in the logs :

# Log possible partials
Rails.logger.info "  Upmin partials lookup: #{partials}"

In this case :

  Upmin partials lookup: ["upmin/partials/attributes/child_parent_id", "upmin/partials/attributes/child_integer", "upmin/partials/attributes/integer", "upmin/partials/attributes/unknown"]