dav_i
dav_i

Reputation: 28097

Only allow creation of has_many in ActiveAdmin

I have a has_many relation for which I have disabled destroying:

unless f.object.new_record?
  f.inputs do
    f.has_many :foos, allow_destroy: false do |foo|
      foo.input :bar
    end
  end
end

Currently once I've saved the main object and go back and edit, I can edit previously created foos. I'd like this to not be the case and only have the ability to add new ones. How can I achieve this?

I can see ActiveAdmin provides allow_destroy and new_record, but there isn't something analogous to allow_edit.

Upvotes: 0

Views: 672

Answers (1)

dav_i
dav_i

Reputation: 28097

Turns out the answer was staring me in the face: new_record?. Simply check whether the has_many item is a new record or not:

f.inputs do
  f.has_many :foos, allow_destroy: false do |foo|
    if foo.object.new_record?
      foo.input :bar
    end
  end
end

Upvotes: 2

Related Questions