English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app

Namespacing in Rails


With namespace routes in Rails you can easily create a prefix for select resources. For instance, if you have an admin area or an account dashboard you can give logged in users access to methods that are not otherwise available. To use namespaces define them in config/routes.rb. *Note: this is available for the latest release of Rails, at this time it is 2.3.0. I think it's supported back until Rails 2.0, but haven't tested it.

In your config/routes.rb file

map.namespace :dashboard do |dashboard|
    dashboard.resources :properties, :collection=>{:available => :get}
This namespace will create routes such as dashboard/properties, dashboard/properties/1, essentially all the CRUD paths as well as a collection method "available" dashboard/properties/available. These routes can be accessed in your views with methods such as new_dashboard_property_path, available_dashboard_properties_path  etc. If your property model has it's own controller that isn't namespaced, properties_controller.rb, remember to modify the property form_for method to tell it to use the dashboard namespace. The normal form_for(@property), method takes just your property object. Instead pass the dashboard symbol inside of an array  to form_for
<% form_for [:dashboard, @property] do |f| %><% end %>
You can easily see all of the available paths from the command line using rake routes. I use grep to filter the results for conveinence, because on a large application I don't necessarily need to see all the routes if I'm working just within a single namespace. rake routes | grep dashboard

Tagged w/ #grep #namespace #rake #routesruby on rails