I’m really new to programming, so I’m having trouble explaining this — please forgive.
I have a Document model and a Note model in my rails app. A note belongs to a document, and a document has many notes — the foreign key in the notes table is document_id.
On my document show page, I have a form for a note which uses a :content attribute as a text_area field.
What I’d like to do is pass the document’s id into the note params so the note would have both the :content the user submits alng with the :document_id based on the document_path.
Currently I’m adding the :document_id into the note’s params hash using a hidden_field form helper, and sending the whole thing to the NotesController, but I hope there’s a cleaner / perhaps easier way.
If this makes sense, can someone suggest a better way to do this? Thank you.
In your routes have something like
Then you should be adding a note via this route
/documents/5/notes/new
Then in your NotesController have
(In no way has this been tested – but it gives you an idea of how to do it in a RESTFUL style without hidden fields)