so i have got two models with each one controller:
Model Project has_many Themes
Model Theme belongs_to Project
in my routes file i added resources :projects and also for themes. Now i can add a project with localhost/projects/new which works fine and i can add Themes with localhost/themes/new. But thats not the way i want.
I only want to add Themes related to a project. Whats the best way to do this? I tried something like this: match "projects/:project_id/themes/new" => 'themes#new', :as => 'themes' which seems to work, but after submitting my new form nothing happens. the new form gets rendered again without a error message or something like that. my form tag in html gets rendered as the following:
<form accept-charset="UTF-8" action="/projects/3/themes/new" class="new_theme" id="new_theme" method="post">
do you have any ideas what went wrong? is there a best practice for something like that, because i think its a often wanted model.
This situation is called nested resources.
In your routes define:
It will create exactly the url you described, as well as a bunch of helper methods. See the rails guides for a complete list, but here’s an example:
To reach a single theme you would use
projects_theme_path(@project, @theme), or to view all the themes for a project you would useprojects_themes_path(@project). Again, see the rails guides for the full explanation and all the helpers.Also, at any time you can run
rake routesto see the EXACT helper methods as they are set up for your project.