I have a problem with the normal way rails operates when using nested forms / resources and routing.
I have two tables, Words and Definitions...
Words have many definitions, but I do not create a Word until it has at least one definition.
Everything on the model and controller end works but I cannot figure out how to handle the form helpers.
<%= semantic_form_for [@word, @definition] do |f| %>
This works perfectly but only if @word actually exists and is not a new UNSAVED record. IE in the controller I am doing a find_or_initialize_by
call for Word then building a definition off of that.
<%= semantic_form_for [:word, @definition] do |f| %>
This words but only if the word doesn't exist. IE if I try to edit using this construction I get an odd url (which doesn't work). words/12345/definition/12345
I tried using the url_for
helper but had similar results as above...
Any other ideas?
Mongoid doesn't initialize embedded documents by default. You need to build them yourself most likely with a callback in your Word
model:
after_initialize :build_definition
def build_definition
self.definitions.build unless self.definitions.any?
end