method

namespace

Importance_2
v2.3.8 - Show latest stable - 1 note - Class: ActionController::Routing::RouteSet::Mapper
  • 1.0.0
  • 1.1.6
  • 1.2.6
  • 2.0.3
  • 2.1.0 (0)
  • 2.2.1 (0)
  • 2.3.8 (0)
  • 3.0.0
  • 3.0.9
  • 3.1.0
  • 3.2.1
  • 3.2.8
  • 3.2.13
  • 4.0.2
  • 4.1.8
  • 4.2.1
  • 4.2.7
  • 4.2.9
  • 5.0.0.1
  • 5.1.7
  • 5.2.3
  • 6.0.0
  • 6.1.3.1
  • 6.1.7.7
  • 7.0.0
  • 7.1.3.2
  • 7.1.3.4
  • What's this?
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.

Show source
Register or log in to add new notes.
May 27, 2010
2 thanks

Use :path_prefix for the namespace

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"