method

namespace

namespace(name, options = {}, &block)
public

Enables the use of resources in a module by setting the name_prefix, path_prefix, and namespace for the model. Example:

  map.namespace(:admin) do |admin|
    admin.resources :products,
      :has_many => [ :tags, :images, :variants ]
  end

This will create admin_products_url pointing to "admin/products", which will look for an Admin::ProductsController. It’ll also create admin_product_tags_url pointing to "admin/products/#{product_id}/tags", which will look for Admin::TagsController.

1Note

Use :path_prefix for the namespace

allen ยท May 27, 20102 thanks

Resources are added after the :path_prefix. However if you use a :path_prefix on a resource, it overrides the namespace path instead of appending to it (as I think it should).

Here is what I wrote to create a versioned API access path.

map.namespace :api3, :path_prefix=>"/api/v3" do |api| api.resources :posts api.resources :comments, :path_prefix=>"/api/v3/post/:post_id" end

This will create routes like path: /api/v3/posts/1 named_route: api3_post() controller=>"api3/posts"