2

I'm getting started on a large Rails project that will consist of several independent areas including and admin area, front-end website, user area, and api.

Would it be wise to create say, 3 namespaces for these sections (leaving the front-end website as the root)?

There doesn't seem to be a general rule regarding namespaces and it seems like the right choice for organizational purposes since each area would require several controllers.

In my past Rails projects I always namespace the admin, but in this case, are there any drawbacks to having multiple namespaces in the long run (in regards to routing / interacting with other namespaces)?

Any advice would be much appreciated!

3 Answers 3

5

no drawbacks only advantages if the namespaces make sense. Obviously having too many namespaces might be confusing and pointless but breaking up admin/client/public is a pretty common use case for using namespaces.

just make sure you give all your controllers unique names, so you don't run into the problem this guy ran into with namespaces: http://blog.philburrows.com/articles/2008/05/02/rails-namespaces-rock-rails-namespaces-suck-/

Sign up to request clarification or add additional context in comments.

1 Comment

Oh man, I've been using colliding controller names in different namespaces for a while with no problem. Kind of scares me that this kind of issue can come up. Perhaps the problem has been solved in Rails 3?
3

If your project really does consist of logically independent areas then I don't see a downside. Any routing situation can be implemented with minimal effort, so that's not a dealbreaker.

One think you will have to keep in mind is that your project is going to have one extra level of directory nesting. For example, you can't just look in the views directory and get a good idea of all of your views, because they will all live in directories corresponding to their namespace (the same goes for tests). This is often desired, however, as it helps things stay a little more organized.

2 Comments

Great! Sounds like namespaces would be perfect for this project. Would sharing the ApplicationController across namespaces prove to be tricky? Is it common practice to create new inheritance controllers within namespaces (for example if the admin needs separate authentication filters, etc...)? Thanks for your advice!
Not at all; all controllers still subclass ApplicationController, regardless of whether or not they're in a namespace: class YourNamespace::YourController < ApplicationController
0

checkout http://www.ror-e.com and the github app https://github.com/drhenner/ror_ecommerce It has several namespaces and most namespaces have their own base_controller.

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.