【问题标题】:Active Admin Nested models not populating in edit未在编辑中填充活动的管理员嵌套模型
【发布时间】:2014-09-09 15:24:22
【问题描述】:

所以,我一直在修改 Active Admin,到目前为止,尽管文档不是很好,但我一直在航行。但是当我尝试编辑具有一些 belongs_to 关联的模型时遇到了一个问题。该表单没有填充这些关联的记录......并且由于 Active Admin 通过魔术做了很多事情,我不知道问题可能是什么,因为正如我所说,Active 的文档有些缺乏管理员,就我的搜索而言,没有人遇到过类似的问题......所以,我对想法持开放态度。

我得到了所有情侣属性的记录,但婚礼、新娘和新郎什么都没有

models/couple.rb

class Couple < ActiveRecord::Base
 after_create :set_full_name
 belongs_to :wedding, :dependent => :destroy
 belongs_to :groom, :class_name => "Partner::Groom", :dependent => :destroy
 belongs_to :bride, :class_name => "Partner::Bride", :dependent => :destroy
 accepts_nested_attributes_for :groom, :bride, :wedding
 attr_accessible :bride, :groom, :bride_id, :groom_id, :notification_email, :contact_address,     :comment,:bride_attributes, :groom_attributes, :wedding_attributes
 validates :notification_email, email: true, uniqueness: true, presence: true
 validates :contact_address, presence: true
 validates :wedding, :groom, :bride, presence: true
end

admin/couples.rb

ActiveAdmin.register Couple do
  menu :priority => 3, url: ->{ admin_couples_path(locale: I18n.locale) }, :label => proc{I18n.t('general.couples')}

  scope proc{I18n.t('admin.couples.scope.all')},:all, :default => true
  scope proc{I18n.t('admin.couples.scope.past_week')},:past_week do |couples|
    couples.where('created_at >= ? and created_at <= ?', Time.now - 1.week, Time.now)
  end
  scope proc{I18n.t('admin.couples.scope.last_month')},:last_month do |couples|
    couples.where('created_at >= ? and created_at <= ?', Time.now - 1.month, Time.now)
  end

  form do |c|
   c.inputs "Couples" do
       c.input :contact_address, :label => t('admin.couples.attributes.contact_address')
       c.input :notification_email, :label => t('admin.couples.attributes.notification_email')
       c.input :comment, :label => t('admin.couples.attributes.comment')
   end
   c.inputs "Groom" do
     c.inputs :for => :groom_attributes do |g|
       g.input :first_name, :label => t('general.first_name')
       g.input :last_name, :label => t('general.last_name')
       g.input :email, :label => t('general.email')
       g.input :date_of_birth, as: :date_select, start_year: Date.today.year - 10, end_year: Date.today.year - 80, :label => t('general.date_of_birth')
       g.input :dni
       g.input :mobile_number, as: :phone, :label => t('general.mobile_number')
       g.input :phone_number, as: :phone, :label => t('general.phone_number')
       g.input :facebook
       g.input :twitter
     end
   end
   c.inputs "Bride" do
     c.inputs :for => :bride_attributes do |b|
       b.input :first_name, :label => t('general.first_name')
       b.input :last_name, :label => t('general.last_name')
       b.input :email, as: :email, :label => t('general.email')
       b.input :date_of_birth, :label => t('general.date_of_birth'), as: :date_select, start_year: Date.today.year - 10, end_year: Date.today.year - 80
       b.input :dni
       b.input :mobile_number, as: :phone, :label => t('general.mobile_number')
       b.input :phone_number, as: :phone, :label => t('general.phone_number')
       b.input :facebook
       b.input :twitter
     end
   end

    c.inputs "Wedding" do
      c.inputs :for => :wedding_attributes do |w|
        w.input :date, as: :date_select, start_year: Date.today.year, end_year: Date.today.year + 10,:label => t('admin.weddings.date')
        w.input :ceremony_time, as: :time_select, :label => t('admin.weddings.ceremony_time')
        w.input :ceremony_place, :label => t('admin.weddings.ceremony_place')
        w.input :civil_ceremony_date, as: :date_select, start_year: Date.today.year, end_year: Date.today.year + 10, :label => t('admin.weddings.civil_ceremony_date')
        w.input :civil_ceremony_time, as: :time_select, :label => t('admin.weddings.civil_ceremony_time')
        w.input :civil_ceremony_place, :label => t('admin.weddings.civil_ceremony_place')
        w.input :facebook_notifications_enabled, as: :boolean
        w.input :twitter_notifications_enabled, as: :boolean
        w.input :number_of_guests, :label => t('admin.weddings.number_of_guests')
        w.input :site_id, as: :select, :collection => Site.all.map{|u| ["#{u.country}", u.id]}
      end
    end
    c.actions
  end

  index do |c|
    c.column :id
    c.column t('admin.couples.full_name'), :full_name,:sortable => :full_name  do |couple|
      couple.full_name
    end
    c.column t('admin.couples.attributes.notification_email'),:notification_email
    c.column t('admin.couples.attributes.contact_address'),:contact_address
    c.default_actions
  end

end

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3.2 activeadmin


    【解决方案1】:

    所以,我发现了问题所在......既然我已经修复了它,我想把我发现的问题留在这里,所以如果其他人遇到同样的问题,他们会有答案。

    显然我为嵌套模型构建了错误的表单,而不是

    c.inputs :for => :groom_attributes do |g|
    

    我用过

    c.inputs for: [:groom, c.object.groom || c.object.build_groom] do |g|
    

    admin/couples.rb

    ActiveAdmin.register Couple do
      menu :priority => 3, url: ->{ admin_couples_path(locale: I18n.locale) }, :label => proc{I18n.t('general.couples')}
    
      scope proc{I18n.t('admin.couples.scope.all')},:all, :default => true
      scope proc{I18n.t('admin.couples.scope.past_week')},:past_week do |couples|
        couples.where('created_at >= ? and created_at <= ?', Time.now - 1.week, Time.now)
      end
      scope proc{I18n.t('admin.couples.scope.last_month')},:last_month do |couples|
        couples.where('created_at >= ? and created_at <= ?', Time.now - 1.month, Time.now)
      end
    
      form do |c|
        c.semantic_errors *c.object.errors.keys
       c.inputs "Couples" do
           c.input :contact_address, :label => t('admin.couples.attributes.contact_address')
           c.input :notification_email, :label => t('admin.couples.attributes.notification_email')
           c.input :comment, :label => t('admin.couples.attributes.comment')
       end
       c.inputs "Groom" do
         c.inputs for: [:groom, c.object.groom || c.object.build_groom] do |g|
           g.input :first_name, :label => t('general.first_name')
           g.input :last_name, :label => t('general.last_name')
           g.input :email, :label => t('general.email')
           g.input :date_of_birth, as: :date_select, start_year: Date.today.year - 10, end_year: Date.today.year - 80, :label => t('general.date_of_birth')
           g.input :dni
           g.input :mobile_number, as: :phone, :label => t('general.mobile_number')
           g.input :phone_number, as: :phone, :label => t('general.phone_number')
           g.input :facebook
           g.input :twitter
         end
       end
       c.inputs "Bride" do
         c.inputs for: [:bride, c.object.bride || c.object.build_bride] do |b|
           b.input :first_name, :label => t('general.first_name')
           b.input :last_name, :label => t('general.last_name')
           b.input :email, as: :email, :label => t('general.email')
           b.input :date_of_birth, :label => t('general.date_of_birth'), as: :date_select, start_year: Date.today.year - 10, end_year: Date.today.year - 80
           b.input :dni
           b.input :mobile_number, as: :phone, :label => t('general.mobile_number')
           b.input :phone_number, as: :phone, :label => t('general.phone_number')
           b.input :facebook
           b.input :twitter
         end
       end
    
        c.inputs "Wedding" do
          c.inputs for: [:wedding, c.object.wedding || c.object.build_wedding] do |w|
            w.input :date, as: :date_select, start_year: Date.today.year, end_year: Date.today.year + 10,:label => t('admin.weddings.date')
            w.input :ceremony_time, as: :time_select, :label => t('admin.weddings.ceremony_time')
            w.input :ceremony_place, :label => t('admin.weddings.ceremony_place')
            w.input :civil_ceremony_date, as: :date_select, start_year: Date.today.year, end_year: Date.today.year + 10, :label => t('admin.weddings.civil_ceremony_date')
            w.input :civil_ceremony_time, as: :time_select, :label => t('admin.weddings.civil_ceremony_time')
            w.input :civil_ceremony_place, :label => t('admin.weddings.civil_ceremony_place')
            w.input :facebook_notifications_enabled, as: :boolean
            w.input :twitter_notifications_enabled, as: :boolean
            w.input :number_of_guests, :label => t('admin.weddings.number_of_guests')
            w.input :site_id, as: :select, :collection => Site.all.map{|u| ["#{u.country}", u.id]}
          end
        end
        c.actions
      end
    
      index do |c|
        c.column :id
        c.column t('admin.couples.full_name'), :full_name,:sortable => :full_name  do |couple|
          couple.full_name
        end
        c.column t('admin.couples.attributes.notification_email'),:notification_email
        c.column t('admin.couples.attributes.contact_address'),:contact_address
        c.default_actions
      end
     #
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-18
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      • 2015-11-08
      相关资源
      最近更新 更多