How can I customize only a field in rails_admin?
Asked Answered
H

5

20

I have a field for which rails_admin generates a textfield, but I'd like it to use a <select> tag instead. I customized the field like this, in my initializer:

RailsAdmin.config do |config|
  config.model User do
    update do
      field :state do
        partial "user_state_partial"
      end
    end
  end
end

I've tested it, and it works. The problem is, by doing like this ( I tried with an edit block too ), the only field left, is the one I'm customizing. Is there any way of telling rails_admin to just assume the defaults for the other fields?

Hippie answered 2/4, 2011 at 16:35 Comment(0)
R
2

Once you have defined one field, you have to define all fields that you want to use. The default is all fields.

RailsAdmin.config do |config|
  config.model User do
    update do
      field :name
      field :surname
      field :state do
        partial "user_state_partial"
      end
    end
  end
end
Rebecarebecca answered 20/5, 2011 at 12:44 Comment(0)
M
22

A better (and shorter) solution is to use the 'configure' syntax instead of 'field'. By using configure, rails_admin will use the defaults for all other values.

So for example, using the following:

RailsAdmin.config do |config|
  config.model User do
    update do
      configure :state do
        partial "user_state_partial"
      end
    end
  end
end

...will allow RailsAdmin to use the stated partial for :state, but it will use defaults for all other fields.

More information can be found at: Rails Admin wiki

Millrace answered 6/3, 2012 at 15:2 Comment(0)
U
3

The current docs say you can, like this:

field :name do
  # snipped specific configuration for name attribute
end

include_all_fields # all other default fields will be added after, conveniently
exclude_fields :created_at # but you still can remove fields

...but it still removes association subforms. (You can add back belongs_to items with "field :association_id" (not "field :association") but I'm not sure how to add back has_* subforms.

Unseasoned answered 20/7, 2011 at 1:20 Comment(0)
R
2

Once you have defined one field, you have to define all fields that you want to use. The default is all fields.

RailsAdmin.config do |config|
  config.model User do
    update do
      field :name
      field :surname
      field :state do
        partial "user_state_partial"
      end
    end
  end
end
Rebecarebecca answered 20/5, 2011 at 12:44 Comment(0)
S
1

I usually do include_all_fields, then custom config for my field and then add exclude_fields (for fields like id and timestamps).

Stepha answered 13/1, 2016 at 1:9 Comment(0)
M
0

Their documentation explain this behavior quite well:

By default all fields found on your model will be added to list/edit/export views, if no field is found for the section and model.

But after you specify your first field with field(field_name, field_type = found_column_type, &conf_block) or include_field or fields, this behaviour will be canceled.

Only the specified fields will be added. If you don't want that very behavior, use configure instead of field (same signature). That way, that field won't be added to the section, just configured.

Once in add specified fields mode, you can exclude some specific fields with exclude_fields & exclude_fields_if:

Miller answered 4/5, 2022 at 19:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.