I am building a web app which has multiple projects. The general data model is such that each project has many resources such as documents, registers, etc.Something along the lines of:
class Project < ActiveRecord::Base
has_many :documents, :registers, :employments
has_many :users, :through => :employments
class User < ActiveRecord::Base
has_many :employments
has_many :projects, :through => :employments
class Document < ActiveRecord::Base
belongs_to :project
class Register < ActiveRecord::Base
belongs_to : project
The difficulty comes with routing!! Any C UD actions to projects will be done through a namespace. However, when a user is viewing a project, i want the project_id in the routes such that:
'0.0.0.0:3000/:project_id/documents/
OR
'0.0.0.0:3000/:project_id/register/1/new
I thought about something like:
match '/:project_id/:controller/:id'
I presume I am to store the project_id in a session? If i forgo these routes for something simplier such as just:
"0.0.0.0:3000/documents"
How do I then bind any CRUD actions to documents or registers to the current project?? Surely I don't have to hard wire this into every controller?
HELP!
I guess what you need are nested resources.
Now you'll get routing like this:
You'll be able to call
params[:project_id]
inside the DocumentsController and RegistersController. You won't need a session to store the project_id. This will be available to you inside the url. You should avoid sessions as much as possible when creating a RESTful application.The only extra thing you need to do is setting the relationship inside the create action of both Controllers:
What I like to do is creating a helper method inside the ApplicationController that gets the current project.
Now you can do the following inside the create action:
You'll also be able to call
current_project
inside your views for registers and documents. Hope that helps you out!Check the Ruby on Rails guide for some more information on nested resources: http://edgeguides.rubyonrails.org/routing.html#nested-resources