【问题标题】:Configure the label of Active Admin has_many配置 Active Admin has_many 的标签
【发布时间】:2011-11-29 12:50:04
【问题描述】:

嗯,我有两个与多对多关联相关的模型。

#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,但这些选项都不起作用,我也尝试过:图例、:title 和 :label,而不是 .has_many 中的 :name。不工作。

这是该代码的结果: Screenshot

我要显示的是“文档版本”而不是“文档”,以及“添加新文档版本”而不是“添加新文档”

如果有人能提供解决方案,那就太好了

【问题讨论】:

  • 我有同样的问题,我在问题跟踪器中发布了它。也许您可以向其添加更多信息:github.com/gregbell/active_admin/issues/786
  • 看来你不能:form_buffers.last &lt;&lt; template.content_tag(:h3, association.to_s.titlecase)(来自活动管理员 form_builder.rb)

标签: ruby ruby-on-rails-3 formtastic activeadmin formbuilder


【解决方案1】:

要设置has_many 标头,您可以使用

f.has_many :images, heading: 'My images' do |i|
  i.input :src, label: false
end

here

【讨论】:

    【解决方案2】:

    查看ActiveAdmin tests("应该翻译标题中的关联名称"),可能还有另一种方法。使用您的翻译文件。

    如果您查看ActiveAdmin has_many method(糟糕!!!46 行顺序代码),它使用的是ActiveModel's human method

    尝试将此添加到您的翻译文件中

    en:
      activerecord:
        models:
          document:
            one: Document Version
            other: Document Versions
    

    【讨论】:

    • 这是处理在表单中更改类名称的最简单方法。有我所有的赞成票。
    • 这不是最佳答案吗?就是这么简单!谢谢。
    【解决方案3】:

    一个快速的技巧是您可以通过样式隐藏 h3 标签。

    资产/样式表/active_admin.css.scss

        .has_many {
          h3 {
            display: none;
          }}
    

    这将隐藏 has_many 类下的任何 h3 标签。

    【讨论】:

      【解决方案4】:

      您可以使用has_many 上的new_record 设置自定义“添加...”按钮的标签。对于标题标签,您可以使用heading:

      f.has_many :documents,
                 heading: "Document Versions",
                 new_record: "Add new Document Version" do |d|
        d.input :file, :as => :file
      end
      

      【讨论】:

        【解决方案5】:

        Sjors 的回答实际上是解决问题的完美开始。我在 config/initializers/active_admin.rb 中对 Active Admin 进行了猴子补丁:

        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
        

        现在在我的管理文件中,我像 has_many 一样调用 titled_has_many,但我传入 :header 以覆盖使用 Association 作为 h3 标签。

        f.titled_has_many :association, header: "Display this as the H3" do |app_f|
          #stuff here
        end
        

        【讨论】:

        • 谢谢老兄!如果我有时间,我会尝试将该修复应用到 active_admin repo
        • 但这里还有另一个问题 - 我们无法重命名“添加更多资源”按钮:/
        【解决方案6】:

        不值得奖励,但您可以将其放在 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 
        

        【讨论】:

          猜你喜欢
          • 2012-05-16
          • 2013-05-02
          • 1970-01-01
          • 2014-09-19
          • 1970-01-01
          • 2014-04-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多