Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field::BelongsTo > field.associated_resource_options seems to produce only id's for the key field #2382

Closed
rbarisic-lme opened this issue May 14, 2023 · 4 comments

Comments

@rbarisic-lme
Copy link

rbarisic-lme commented May 14, 2023

  • What were you trying to do?
    I was trying to display a list of associated items on a belongs_to element (post.rb, trying to display profile.rb)
    the post belongs to the profile

  • What did you end up with (logs, or, even better, example apps are great!)?
    field.associated_resource_options looks something like this:
    [["Profile #2290", 2290], ["Profile #3719", 3719], ["Profile #3698", 3698], ["Profile #2786", 2786], ["Profile #2793", 2793], ["Profile #3815", 3815], ["Profile #2657", 2657], ["Profile #2834", 2834], ["Profile #2836", 2836], ["Profile #2105", 2105], ["Profile #3317", 3317], ["Profile #2287", 2287], ["Profile #4107", 4107], ["Profile #2289", 2289], ["Profile #2839", 2839], ["Profile #2097", 2097], ["Profile #2313", 2313], ["Profile #2304", 2304], ["Profile #3224", 3224], ["Profile #3756", 3756], ["Profile #3757", 3757], ["Profile #3828", 3828], ["Profile #2849", 2849], ["Profile #3547", 3547], ["Profile #3543", 3543], ["Profile #3548", 3548], ["Profile #3549", 3549], ["Profile #3550", 3550], ["Profile #3551", 3551], ["Profile #3552", 3552], ["Profile #4097", 4097], ["Profile #3544", 3544], ["Profile #3545", 3545], ["Profile #3546", 3546], ["Profile #2794", 2794], ["Profile #3090", 3090], ["Profile #2796", 2796], ["Profile #2821", 2821], ["Profile #2859", 2859], ["Profile #4098", 4098], ["Profile #3758", 3758], ["Profile #3759", 3759], ["Profile #3760", 3760], ["Profile #3762", 3762], ["Profile #2169", 2169], ["Profile #2869", 2869], ["Profile #2173", 2173], ["Profile #4044", 4044], ["Profile #2174", 2174], ["Profile #2321", 2321], ["Profile #2172", 2172], ["Profile #2878", 2878], ["Profile #3691", 3691], ["Profile #3692", 3692], ["Profile #3694", 3694], ["Profile #3695", 3695], ["Profile #3696", 3696], ["Profile #3697", 3697], ["Profile #3822", 3822], ["Profile #2175", 2175], ["Profile #2879", 2879], ["Profile #3092", 3092], ["Profile #2322", 2322], ["Profile #4099", 4099], ["Profile #2447", 2447], ["Profile #2882", 2882], ["Profile #3553", 3553], ["Profile #3556", 3556], ["Profile #3681", 3681]]

  • What versions are you running?

    • Rails 7.0.4.3
    • administrate 0.18.0

I just want to display a list of the profiles by their names. They do have a name attribute in their model.

From Post Dashboard:

  ATTRIBUTE_TYPES = {
    id: Field::Number,
    title: Field::String,
    content: RichTextAreaField,
    profile: Field::BelongsTo,
    teaser: RichTextAreaField,
    editorial: Field::Boolean,
    created_at: Field::DateTime,
    updated_at: Field::DateTime,
    published_at: Field::DateTime,
  }.freeze

this is my belongs_to/_form.html.erb:

<div class="field-unit__label">
  <%= f.label field.permitted_attribute %>
</div>

<div class="field-unit__field">
  <%= f.select(field.permitted_attribute,
               options_for_select(field.associated_resource_options, field.selected_option),
               include_blank: field.include_blank_option) %>
</div>

<%= field.associated_resource_options %>
@rbarisic-lme rbarisic-lme added the bug breakages in functionality that is implemented label May 14, 2023
@rbarisic-lme
Copy link
Author

image

@rbarisic-lme
Copy link
Author

rbarisic-lme commented May 14, 2023

nvm, I found it this snippet in the dashboard code:

  # Overwrite this method to customize how profiles are displayed
  # across all pages of the admin dashboard.
  #
  # def display_resource(profile)
  #   "Profile ##{profile.id}"
  # end

I edited it like so:

  def display_resource(profile)
    profile.name
  end

And now it's working.
It would be nice to add it to the documentation of belongs_to, because I spent quite some hours finding this

@Juxlarry
Copy link

Awesome discovery

@pablobm pablobm removed the bug breakages in functionality that is implemented label May 19, 2023
@pablobm
Copy link
Collaborator

pablobm commented May 19, 2023

This is in the documentation, on the same page http://administrate-demo.herokuapp.com/customizing_dashboards (under "Defining labels" further below). However I understand how this can be difficult to find. In general writing documentation is difficult because not everything can be made discoverable, as there would be too much information and things would be hidden again.

Having said that, do you have a suggestion as to how we could make this clearer? A PR would be fantastic.

I'm closing this as it's documented, while we still would accept a contribution to improve it.

@pablobm pablobm closed this as completed May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants