0

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?

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

It works, but I noticed later it causes a lot of other problems too. I can no longer use @word.definitions.blank?
0

If you wanna stay CRUD and allow definitions to be created before words, you must duplicate routes for definitions, one inside words and one outside, so you can do:

<%= semantic_form_for [@definition] do |f| %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.