1

Trying to get a form for adding, editing and removing embedded documents working as described in this question: Rails + MongoMapper + EmbeddedDocument form help.

I've got it mostly to work, however, when editing a document, if all embedded documents are removed by clicking the remove link the params object that is passed to the controller does not include the array field at all, so update_attributes ignores it thinking that it hasn't been changed, so doesn't actually remove the embedded documents at all.

Has anyone else encountered this? And other than a hacky way of dealing with it on a case by case basis, is there a nice, neat solution?

Community
  • 1
  • 1
Russell
  • 12,261
  • 4
  • 52
  • 75

1 Answers1

0

That's more of a params problem than a MongoMapper problem.

Building off the code in Rails + MongoMapper + EmbeddedDocument form help...

You could try adding a hidden field before all the answers so an array will always come through the params...

<%= hidden_field_tag 'problem[answers][]' %>

And that will come through in your params like...

{ "problem" => { "answers" => [""] }

But that'll probably error, so perhaps a better way is to just do this in your controller before the update_attributes...

params[:problem][:answers] ||= []
Community
  • 1
  • 1
Brian Hempel
  • 8,844
  • 2
  • 24
  • 19
  • Your second option is basically the hacky way of dealing with it on a case by case basis that I envisaged. – Russell Feb 17 '12 at 09:28