Active Admin嵌套模型未在编辑中填充

时间:2014-09-09 15:24:23

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

所以,我一直在修补Active Admin,到目前为止,虽然文档不是很好,但我一直在航行。但是当我尝试使用一些belongs_to关联编辑模型时遇到了一个问题。表单没有填充这些关联的记录......由于Active Admin通过魔术做了很多事情,我不知道问题是什么,因为正如我所说,文档在某些方面缺乏Active管理员和我的搜索,没有人有类似的问题...所以,我愿意接受想法。

我得到所有情侣属性的记录,但是对于Wedding,Bride&什么都不新郎

模型/ 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

管理员/ 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

1 个答案:

答案 0 :(得分:1)

所以,我发现了问题是什么......自从我修复它之后,我想留下我在这里发现的东西,所以如果其他人面临同样的问题,他们就会有答案。

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

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

我用过

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

<强> 管理员/ 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