Following my previous post Mongoid custom relation between embedded documents
Interesting how to set-up Routes and MVC for embedded documents? I'll try to tell you how I did it.
As I mentioned in previous post, my main document Event looks like
1 2 3 4 5 | |
Routing
In routes.rb
1 2 3 4 | |
Now we can access the Acts and Areas with urls like /events/:event_id/acts/, /events/:event_id/acts/:id, and same for areas
To see the all the routes you can use command rake routes in terminal
Controllers
In all controllers for embedded documents, we need to have access to parent document. We can achieve this by adding before_filter to our controllers.
1 2 3 4 5 6 7 8 9 10 | |
And also we need to change the actions to use our parent document
1 2 3 4 5 6 7 8 9 | |
Also, we need to change the creation method, so the child document will be added to the parent
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Notice the adding code @event.acts << @act and the redirect code redirect_to [@event, @act]
Views
For every link_to in your view, you need to add relation to Event document, and also some paths needs changing.
1 2 3 | |
For forms, I'm using simple_form gem, but basicaly the only thing you need to change is to put the @event there
1 2 | |
I hope this short post will help you getting started…