Replies: 2 comments
-
|
We can use |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Here is what I am doing using the custom field option for arrays (working with both string and int arrays): # /app/madmin/fields/array_field.rb
class ArrayField < Madmin::Field
def value(record)
record.public_send(attribute_name)&.join(", ")
end
# def to_partial_path(name)
# unless %w[index show form].include? name
# raise ArgumentError, "`partial` must be 'index', 'show', or 'form'"
# end
#
# "/madmin/fields/#{self.class.field_type}/#{name}"
# end
def to_param
{ attribute_name => [] }
end
# # Used for checking visibility of attribute on an view
# def visible?(action, default: true)
# options.fetch(action.to_sym, default)
# end
# def required?
# model.validators_on(attribute_name).any? { |v| v.is_a? ActiveModel::Validations::PresenceValidator }
# end
endI'm making use of the existing stimulus controller for nested forms (even though it is off semantically), which is much nicer than fiddling around in just one input: #/app/views/madmin/fields/array_field/_form.erb
<div data-controller="nested-form" data-nested-form-wrapper-class="array-item" class="md:flex">
<div class="block md:inline-block md:w-32 flex-shrink-0 text-gray-700">
<%= render "madmin/shared/label", form: form, field: field %>
</div>
<% array = form.object.public_send(field.attribute_name) || [] %>
<div class="flex flex-col">
<% array.each_with_index do |value, index| %>
<div class="array-item mb-2" data-new-record="true">
<%= form.text_field "#{field.attribute_name}_#{index}", value: value, class: "form-input", name: "#{form.object_name}[#{field.attribute_name}][]" %>
<button data-action="nested-form#remove_association" type="button" class="px-4 py-2 text-white bg-red-500 rounded hover:bg-red-600">Remove</button>
</div>
<% end %>
<div data-nested-form-target="links">
<button data-action="nested-form#add_association" type="button" class="px-4 py-2 text-white bg-blue-500 rounded hover:bg-blue-600">
Add More
</button>
</div>
</div>
<template data-nested-form-target="template" >
<div class="array-item mb-2" data-new-record="true">
<%= form.text_field "#{field.attribute_name}_template", class: "form-input", name: "#{form.object_name}[#{field.attribute_name}][]" %>
<button data-action="nested-form#remove_association" type="button" class="px-4 py-2 text-white bg-red-500 rounded hover:bg-red-600">Remove</button>
</div>
</template>
</div> |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I love postgres array type. I tend to reach for it for any tagging system that isn't too complex. Most of all, I find it super fun to write code like this:
I just sloppy pasted this from the internet; it does get it done, even if it isn't pretty.
The idea is to tag, for example, blog posts.
When playing around with the possibilities of doing this in Administrate, I discovered a few limitations that would be absolutely amazing if they could be ironed out in madmin.
If there is a way already, please let me know. Also, happy to contribute something if I can.
Beta Was this translation helpful? Give feedback.
All reactions