Activeadmin form with nested has_one inside has_many

时间:2015-09-01 22:50:08

标签: ruby-on-rails nested activeadmin

I am using ActiveAdmin on my Rails project.
I have several nested models like following:
A MapArticle model which has_many Locations (address, city, postcode, …) which has_one Marker (icon and color).

My problem is about the form action in ActiveAdmin. I can’t figured out how to display the has_one marker relation. Everything I tried didn’t seems to work.

MapArticle model:

class MapArticle < ActiveRecord::Base
  belongs_to :post, polymorphic: true
  has_many :locations, as: :locationable, dependent: :destroy
  accepts_nested_attributes_for :locations, reject_if: :all_blank, allow_destroy: true
end

Location model:

class Location < ActiveRecord::Base
  belongs_to :locationable, polymorphic: true
  has_one :marker, as: :markable, dependent: :destroy
  accepts_nested_attributes_for :marker, reject_if: :all_blank, allow_destroy: true
end

Marker model:

class Marker < ActiveRecord::Base
  belongs_to :markable, polymorphic: true
end

The form:

f.inputs t('activerecord.models.map_article.one'), for: [:map_article, f.object.map_article || MapArticle.new] do |map_article|

  map_article.input :online,
                    as: :boolean,
                    hint: I18n.t('form.hint.map_article.online')

  map_article.has_many :locations, heading: false, allow_destroy: true, new_record: t('add.feminin', klass: t('activerecord.models.location.one')) do |location|
    location.input :address
    location.input :city
    location.input :postcode
    location.input :geocode_address,
                 hint: t('form.hint.location.geocode_address'),
                 input_html: { id: 'gmaps-input-address' }
    location.input :latitude,
                 label: false,
                 input_html: { id: 'gmaps-output-latitude', class: 'hide' }
    location.input :longitude,
                 label: false,
                 input_html: { id: 'gmaps-output-longitude', class: 'hide' }

    # This doesn’t work
    location.inputs t('activerecord.models.marker.one'), for: [:marker, location.object.marker || Marker.new] do |marker|
      marker.input :color
      marker.input :icon
    end
  end
end

Thanks for your help !

My Project:

  • Ruby 2.2.2
  • Rails 4.2.4
  • ActiveAdmin 1.0.0 pre1

0 个答案:

没有答案