开发者

How can I customize only a field in rails_admin?

I have a field for which rails_admin generates a textfield, but I'd like it to use a <select> ta开发者_JAVA百科g 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?


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


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.


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


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


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:

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜