Administrate: Custom labels for fields

Created on 3 Feb 2016  路  16Comments  路  Source: thoughtbot/administrate

Hi, i have some models with short name fields and i want to change the label of some with other more verbose, how can i change the labels of some string fields? Thanks all.

Most helpful comment

Sorry for the late. This solution works only on normal attributes but on relations doesn't work:
en.yml

  helpers:
    label:
      control1:
        registro1s: "Registros"
        nombre_documento: "titulo"

Here, the "nombre_documento" is a string attribute of control1 and It works. But "registro1s" is a relation, because control1 has many registro1s and it doesn't work.

Any solution for this?

All 16 comments

+1

You can do this via localization.

config/locales/en.yml

en:
  helpers:
    label:
      admin_user:
        password_confirmation: "Confirmation"

I have tried but does not work :confused:. Do you have any project where you have did it to share?

@Xosmond - I just added a test to verify that @atwoodjw's approach works: https://github.com/thoughtbot/administrate/pull/492

Can you post the code you're using that isn't working?

Sorry for the late. This solution works only on normal attributes but on relations doesn't work:
en.yml

  helpers:
    label:
      control1:
        registro1s: "Registros"
        nombre_documento: "titulo"

Here, the "nombre_documento" is a string attribute of control1 and It works. But "registro1s" is a relation, because control1 has many registro1s and it doesn't work.

Any solution for this?

I am trying this but it does not work. Do I need to create an administrate.en.yml file inside locales or can I just add it to my en.yml file ?

Just in the en.yml , make sure you are not using i18n on your config/application.rb

Ok, In my en.yml file I added this. (school is my model and tuition fee is the attribute )

en:
  helpers:
    label:
      school:
        tuition_fee: "Tuition fee average"

And my dashboard is like this

class SchoolDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    tuition_fee: Field::Number.with_options(prefix: "AED ", decimals: 2),
  }

Is that all I need to make it work ? I am trying to customize the label of the tuition fee attribute.

Have you tried with only "Field::Number" ? maybe the kind of attribute is the key.

It is only working on the form page but it is not working for the show page or index page.

it could be, because the way this is working. I hope they can make a better option to customize labels.

@Xosmond, @KatherineMuedas - ah, I didn't realize that you were talking about the index and show pages.

I've updated #492 to work on all three types of pages. I'll try to get that merged in as soon as possible.

@graysonwright that would be great !! Thanks :)

Closed by #492

The custom labels don't work in the forms as far as I can tell. They really can't either, because the forms render the attribute's key directly <%= f.label field.attribute_key, field.attribute %> like so:

<%#
# HasMany Form Partial

This partial renders an input element for belongs_to relationships.
By default, the input is a collection select box
that displays all possible records to associate with.
The collection select box supports multiple inputs,
and is augmented with [Selectize].

## Local variables:

- `f`:
  A Rails form generator, used to help create the appropriate input fields.
- `field`:
  An instance of [Administrate::Field::HasMany][1].
  Contains helper methods for displaying a collection select box.

[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/HasMany
[Selectize]: http://brianreavis.github.io/selectize.js
%>

<div class="field-unit__label">
  <%= f.label field.attribute_key, field.attribute %>
</div>
<div class="field-unit__field">
  <%= f.select(field.attribute_key, nil, {}, multiple: true) do %>
    <%= options_for_select(field.associated_resource_options, field.selected_options) %>
  <% end %>
</div>

However, if we add the same translate call that the collection partial does we can get what we need...

<%#
# HasMany Form Partial

This partial renders an input element for belongs_to relationships.
By default, the input is a collection select box
that displays all possible records to associate with.
The collection select box supports multiple inputs,
and is augmented with [Selectize].

## Local variables:

- `f`:
  A Rails form generator, used to help create the appropriate input fields.
- `field`:
  An instance of [Administrate::Field::HasMany][1].
  Contains helper methods for displaying a collection select box.

[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/HasMany
[Selectize]: http://brianreavis.github.io/selectize.js
%>

<div class="field-unit__label">
  <% label_text = t(
    "helpers.label.#{f.object.class.name.underscore}.#{field.attribute_key}",
    default: field.attribute.to_s,
  ).titleize %>

  <%= f.label field.attribute_key, label_text %>
</div>
<div class="field-unit__field">
  <%= f.select(field.attribute_key, nil, {}, multiple: true) do %>
    <%= options_for_select(field.associated_resource_options, field.selected_options) %>
  <% end %>
</div>

I'll make a PR with this in the next week or two when I get time. Deploying with my custom fork for now.

Here's an example for number (it's the same for string)

<%#
# Number Form Partial

This partial renders an input element for number attributes.
By default, the input is a text field.

## Local variables:

- `f`:
  A Rails form generator, used to help create the appropriate input fields.
- `field`:
  An instance of [Administrate::Field::Number][1].
  A wrapper around the number pulled from the database.

[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Number
%>

<div class="field-unit__label">
  <% label_text = t(
    "helpers.label.#{f.object.class.name.underscore}.#{field.attribute}",
    default: field.attribute.to_s,
  ).titleize %>

  <%= f.label label_text %>
</div>
<div class="field-unit__field">
  <%= f.text_field field.attribute %>
</div>
Was this page helpful?
0 / 5 - 0 ratings