BJ McDuck
BJ McDuck

Reputation: 164

Mongoid Embeds_many won't save on nested form

I've got an embeds_many association I'm trying to set up which I've done successfully before, but I'm trying to do it all in one nested form and I can't figure it out.

Let's say we have a pocket model:

class Pocket
    include Mongoid::Document
    field :title, type: String
    embeds_many :coins, cascade_callbacks: true
end

and a Coin Model:

class Coin
    include Mongoid::Document
    field :name, type: String
    embedded_in :pocket
end

in my form for the pocket, I'm using:

= f.fields_for @pocket.coins do |coin|
    = coin.text_field :name

My controller is the default scaffolded controller. When I use the console, it saves fine and I can see the new pocket and coin I've created. But when I try to create or update a coin from the form, the pocket saves but the coin remains unchanged.

What am I missing here?

Upvotes: 3

Views: 1545

Answers (1)

abhas
abhas

Reputation: 5213

change your model Pocket to

class Pocket
  include Mongoid::Document
  field :title, type: String
  embeds_many :coins, cascade_callbacks: true
  accepts_nested_attributes_for :coins
end

it will work fine.

Upvotes: 2

Related Questions