好吧,我有一个与一对多关联相关的两个模型。
#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
你可以在admin / outlines.rb中看到我已经尝试在has_many:documents中设置:name,在commit_button中设置:title,但是这两个选项都不起作用,我也尝试过: legend,:title和:label,而不是:.has_many中的名称。不工作。
这是该代码的结果: Screenshot
我想要显示的是“文档版本”而不是“文档”,以及“添加新文档版本”而不是“添加新文档”
如果有人可以提供解决方案,那就太棒了
答案 0 :(得分:14)
要设置has_many
标题,您可以使用
f.has_many :images, heading: 'My images' do |i|
i.input :src, label: false
end
请参阅here
答案 1 :(得分:6)
查看ActiveAdmin tests(“应该在标题中翻译关联名称”),可能还有另一种方法。使用您的翻译文件。
如果你看一下ActiveAdmin has_many method(yuck !!! 46行顺序代码),它会使用ActiveModel's human method。
尝试将此添加到您的翻译文件
en:
activerecord:
models:
document:
one: Document Version
other: Document Versions
答案 2 :(得分:4)
一个快速的黑客是你可以通过它的风格隐藏h3标签。
<强>资产/样式表/ active_admin.css.scss 强>
.has_many {
h3 {
display: none;
}}
这将隐藏has_many类下的任何h3标记。
答案 3 :(得分:2)
Sjors的回答实际上是解决问题的完美开端。我使用以下命令在config / initializers / active_admin.rb中对Active Admin进行了monkeypatched:
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
现在在我的管理文件中,我调用titled_has_many就像has_many一样,但我传入:header以覆盖使用Association作为h3标记。
f.titled_has_many :association, header: "Display this as the H3" do |app_f|
#stuff here
end
答案 4 :(得分:2)
您可以使用new_record
上的has_many
设置自定义“添加...”按钮的标签。对于标题标签,您可以使用heading
:
f.has_many :documents,
heading: "Document Versions",
new_record: "Add new Document Version" do |d|
d.input :file, :as => :file
end
答案 5 :(得分:0)
不值得奖励,但你可以把它放在config / initializers / active_admin.rb中。它允许您使用config / locales / your_file.yml调整所需的标头(您应该自己创建custom_translations条目)。别忘了重启服务器。并在表单构建器中使用f.hacked_has_many。
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
如果您在分段/生产模式下未正确加载区域设置文件时遇到问题,请将其添加到您的application.rb可能会有所帮助(替换:nl代表正确的区域设置)
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