12

さて、私は多対多の連想に関連する2つのモデルを持っています。

#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でわかるように、私はすでに:name、has_many:documents、およびcommit_buttonの:titleを設定しようとしましたが、どちらのオプションも機能しません。また、:legend、: .has_manyの:nameの代わりにtitle、および:label。動作していません。

これはそのコードの結果です: スクリーンショット

表示したいのは、「ドキュメント」ではなく「ドキュメントバージョン」、「新しいドキュメントの追加」ではなく「新しいドキュメントバージョンの追加」です。

誰かが解決策を持つことができればそれは素晴らしいでしょう

4

6 に答える 6

18

has_manyヘッダーを設定するには、

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

こちらをご覧ください

于 2013-09-06T11:18:30.003 に答える
6

ActiveAdminテスト(「ヘッダーの関連付け名を変換する必要があります」)を見ると、これを行う別の方法がある可能性があります。翻訳ファイルを使用してください。

ActiveAdmin has_manyメソッド(yuck !!! 46行のシーケンシャルコード)を見ると、ActiveModelのヒューマンメソッドを使用しています。

これを翻訳ファイルに追加してみてください

en:
  activerecord:
    models:
      document:
        one: Document Version
        other: Document Versions
于 2012-11-22T11:16:27.483 に答える
4

簡単なハックの1つは、h3タグをそのスタイルで非表示にできることです。

アセット/スタイルシート/active_admin.css.scss

    .has_many {
      h3 {
        display: none;
      }}

これにより、has_manyクラスの下のh3タグが非表示になります。

于 2012-03-08T15:10:19.260 に答える
3

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
于 2018-01-04T14:06:01.080 に答える
2

Sjorsの答えは、実際には問題を解決するための完璧なスタートです。config / initializers/active_admin.rbのActiveAdminに次のモンキーパッチを適用しました。

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を渡して、h3タグとしてのAssociationの使用をオーバーライドします。

f.titled_has_many :association, header: "Display this as the H3" do |app_f|
  #stuff here
end
于 2012-07-31T16:07:53.853 に答える
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 
于 2012-04-19T15:45:20.670 に答える