Gregg
Gregg

Reputation: 125

Activeadmin customize controller new action

Can anyone explain how I can customize the new action of Activeadmin controller? I've got classes like this: Room, Option and RoomOption. They are in has_many through association and I want to be able to build RoomOptions for each new Room based on Options present in the database.

I've got code like this:

controller do
    def new
      super
      Option.find_each { |option| resource.room_options.build(option: option)}
    end
end

But it doesn't work because no option is created.

Upvotes: 2

Views: 2822

Answers (1)

Piers C
Piers C

Reputation: 2978

I would use:

def new
  build_resource
  Option.find_each { |option| resource.room_options.build(option: option) }
  new!
end

The room model must accept_nested_attributes for options.

Upvotes: 5

Related Questions