Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I customize only a field in rails_admin?

Tags:

rails-admin

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?

like image 861
Geo Avatar asked Apr 02 '11 16:04

Geo


3 Answers

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

like image 65
michaelh Avatar answered Nov 12 '22 02:11

michaelh


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
like image 35
Apie Avatar answered Nov 12 '22 03:11

Apie


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.

like image 3
Shawn Drost Avatar answered Nov 12 '22 02:11

Shawn Drost