htmlruby-on-railsrubytailwind-cssadministrate

Administrate gem — Pass custom HTML classes into fields


I use Rails 7, Ruby 2.7, the administrate gem for my admin dashboard and tailwindcss for the frontend.

I am trying to pass custom HTML classes into the fields. I have already generated the partials for all field types using the command rails generate administrate:views:field all.

For example, in app\views\fields\string\_form, how could I add to the input field a tailwind class? Below is a part of the generated file.

<div class="field-unit__field">
  <%= f.text_field field.attribute %>
</div>

The doc stipulates that there is an instance method called html_class inherited from Base but I struggle to apply it to the view.

Something like this would be perfect:


Solution

  • Woah ok, this was much easier than what I thought. I had to add the classes this way:

    <div class="field-unit__field">
      <%= f.text_field field.attribute, class: "class1 class2 class3" %>
    </div>