Konfigurieren Sie die Beschriftung des aktiven Administrators HASMANY
-
25-10-2019 - |
Frage
Nun, ich habe zwei Modelle mit einem On-to-Many-Assoc.
#models/outline.rb
class Outline < ActiveRecord::Base
has_many :documents
end
#models/document.rb
class Document < ActiveRecord::Base
belongs_to :outline
end
#admin/outlines.rb
ActiveAdmin.register Outline do
form do |f|
f.inputs "Details" do
f.input :name, :required => true
f.input :pages, :required => true
...
f.buttons
end
f.inputs "Document Versions" do
f.has_many :documents, :name => "Document Versions" do |d|
d.input :file, :as => :file
d.buttons do
d.commit_button :title => "Add new Document Version"
end
end
end
end
end
Wie Sie im Administrator/scurts.rb sehen können, habe ich bereits versucht, den: Namen einrichten, im Has_Many: Dokumente und den Titel in der commit_button, aber keine dieser Optionen funktioniert auch mit: Legende: Titel und: Label, statt: Name im .has_Many. Funktioniert nicht.
Dies ist das Ergebnis dieses Code:Bildschirmfoto
Was ich anzeigen möchte, sind "Dokumentversionen" anstelle von "Dokumenten" und "Neue Dokumentversion hinzufügen" anstelle von "neues Dokument hinzufügen"
Wenn jemand eine Lösung haben kann, wäre es großartig
Lösung
Zu setzen has_many
Header, den Sie verwenden können
f.has_many :images, heading: 'My images' do |i|
i.input :src, label: false
end
Sehen hier
Andere Tipps
Anschauen ActiveDmin -Tests("Sollte den Assoziationsnamen in Header übersetzen"), kann es eine andere Möglichkeit geben, dies zu tun. Verwenden Sie Ihre Übersetzungsdatei.
Wenn Sie sich ansehen ActiveAeDmin Has_Many -Methode (Yuck !!! 46 Zeilen sequentieller Code), es wird verwendet Die menschliche Methode von Activemodel.
Fügen Sie dies Ihrer Übersetzungsdatei hinzu
en:
activerecord:
models:
document:
one: Document Version
other: Document Versions
Ein kurzer Hack ist, dass Sie das H3 -Tag durch seinen Stil verbergen können.
Assets/Stylesheets/Active_admin.css.scsss
.has_many {
h3 {
display: none;
}}
Dadurch wird jedes H3 -Tag unter einer Has_Many -Klasse versteckt.
Sjors Antwort ist eigentlich ein perfekter Start, um die Frage zu lösen. I moneypatched Active Admin in config/Initializer/action_admin.rb mit Folgendem:
module ActiveAdmin
class FormBuilder < ::Formtastic::FormBuilder
def titled_has_many(association, options = {}, &block)
options = { :for => association }.merge(options)
options[:class] ||= ""
options[:class] << "inputs has_many_fields"
# Set the Header
header = options[:header] || association.to_s
# Add Delete Links
form_block = proc do |has_many_form|
block.call(has_many_form) + if has_many_form.object.new_record?
template.content_tag :li do
template.link_to I18n.t('active_admin.has_many_delete'), "#", :onclick => "$(this).closest('.has_many_fields').remove(); return false;", :class => "button"
end
else
end
end
content = with_new_form_buffer do
template.content_tag :div, :class => "has_many #{association}" do
form_buffers.last << template.content_tag(:h3, header.titlecase) #using header
inputs options, &form_block
# Capture the ADD JS
js = with_new_form_buffer do
inputs_for_nested_attributes :for => [association, object.class.reflect_on_association(association).klass.new],
:class => "inputs has_many_fields",
:for_options => {
:child_index => "NEW_RECORD"
}, &form_block
end
js = template.escape_javascript(js)
js = template.link_to I18n.t('active_admin.has_many_new', :model => association.to_s.singularize.titlecase), "#", :onclick => "$(this).before('#{js}'.replace(/NEW_RECORD/g, new Date().getTime())); return false;", :class => "button"
form_buffers.last << js.html_safe
end
end
form_buffers.last << content.html_safe
end
end
end
Jetzt nenne ich in meiner Admin -Datei den Titel "mit dem Titel "_Has_Many" wie Has_Many, aber ich übergeben Sie: Header, um die Verwendung des Verbandes als H3 -Tag zu überschreiben.
f.titled_has_many :association, header: "Display this as the H3" do |app_f|
#stuff here
end
Sie können die Beschriftung der Schaltfläche "Hinzufügen ..." mit der Verwendung der Schaltfläche anpassen new_record
Einstellung has_many
. Für das Überschriftenetikett können Sie verwenden heading
:
f.has_many :documents,
heading: "Document Versions",
new_record: "Add new Document Version" do |d|
d.input :file, :as => :file
end
Verdient keinen Preis, aber Sie können dies in Konfiguration/Initializer/action_admin.rb einfügen. Dadurch können Sie die Header, die Sie mit einer Konfiguration/Orte/your_file.yml möchten, optimieren (Sie sollten den Eintrag Custom_translations selbst erstellen). Vergessen Sie nicht, den Server neu zu starten. Und verwenden Sie den f.hacked_has_many in Ihrem Formular Builder.
module ActiveAdmin
class FormBuilder < ::Formtastic::FormBuilder
def hacked_has_many(association, options = {}, &block)
options = { :for => association }.merge(options)
options[:class] ||= ""
options[:class] << "inputs has_many_fields"
# Add Delete Links
form_block = proc do |has_many_form|
block.call(has_many_form) + if has_many_form.object.new_record?
template.content_tag :li do
template.link_to I18n.t('active_admin.has_many_delete'), "#", :onclick => "$(this).closest('.has_many_fields').remove(); return false;", :class => "button"
end
else
end
end
content = with_new_form_buffer do
template.content_tag :div, :class => "has_many #{association}" do
# form_buffers.last << template.content_tag(:h3, association.to_s.titlecase)
# CHANGED INTO
form_buffers.last << template.content_tag(:h3, I18n.t('custom_translations.'+association.to_s))
inputs options, &form_block
# Capture the ADD JS
js = with_new_form_buffer do
inputs_for_nested_attributes :for => [association, object.class.reflect_on_association(association).klass.new],
:class => "inputs has_many_fields",
:for_options => {
:child_index => "NEW_RECORD"
}, &form_block
end
js = template.escape_javascript(js)
_model = 'activerecord.models.' + association.to_s.singularize
_translated_model = I18n.t(_model)
js = template.link_to I18n.t('active_admin.has_many_new', :model => _translated_model), "#", :onclick => "$(this).before('#{js}'.replace(/NEW_RECORD/g, new Date().getTime())); return false;", :class => "button"
form_buffers.last << js.html_safe
end
end
form_buffers.last << content.html_safe
end
end
end
Wenn Sie Probleme mit Gebietsschema -Dateien haben, die im Staging-/Produktionsmodus nicht gut geladen sind, kann dies zu Ihrer Anwendung hinzufügen. RB kann helfen (Ersatz: NL für das richtige Gebietsschema).
config.before_configuration do
I18n.load_path += Dir[Rails.root.join('config','locales','*.{rb,yml}').to_s]
I18n.locale = :nl
I18n.default_locale = :nl
config.i18n.load_path += Dir[Rails.root.join('config','locales','*.{rb,yml}').to_s]
config.i18n.locale = :nl
config.i18n.default_locale = :nl
I18n.reload!
config.i18n.reload!
end
config.i18n.locale = :nl
config.i18n.default_locale = :nl