mattwallace
mattwallace

Reputation: 4222

How do I create custom rails namespace routes

I'm starting to use routes with namespaces for the first time. I understand the concept of what the following does.

namespace :company do
    namespace :project, defaults:{format: 'json'}  do
      resources :registration 
    end
  end

My controller looks like so

class Company::Project::RegistrationController  < ApplicationController

    before_filter :authenticate_user!
    #responds_to :json

    def register

    end

    def entry

    end
end

So in the resources I would like to define the route for register and entry and I've not found anything that really tells me how to do that. I know that if I wasn't using namespace then I would normally do something like this in my routes file.

match 'company/project/registration/register' => "Registration#register"

Is there a way to do that within the namespace block?

---------- after changes -------------- after making suggested changes below in the first answer this is what running >rake routes gives me

register_company_project_registration POST       /company/project/registration/:id/register(.:format) company/project/Registration#register {:format=>"json"}
   entry_company_project_registration POST       /company/project/registration/:id/entry(.:format)    company/project/Registration#enrty {:format=>"json"}
   company_project_registration_index GET        /company/project/registration(.:format)              company/project/registration#index {:format=>"json"}
                                             POST       /company/project/registration(.:format)              company/project/registration#create {:format=>"json"}
     new_company_project_registration GET        /company/project/registration/new(.:format)          company/project/registration#new {:format=>"json"}
    edit_company_project_registration GET        /company/project/registration/:id/edit(.:format)     company/project/registration#edit {:format=>"json"}
         company_project_registration GET        /company/project/registration/:id(.:format)          company/project/registration#show {:format=>"json"}
                                             PUT        /company/project/registration/:id(.:format)          company/project/registration#update {:format=>"json"}
                                             DELETE     /company/project/registration/:id(.:format)          company/project/registration#destroy {:format=>"json"}

Upvotes: 1

Views: 4464

Answers (1)

thesis
thesis

Reputation: 2565

I hope I understood you right. You want to add additional routes for sub routes?

Approach could be like this:

namespace :company do
   namespace :project, defaults:{format: 'json'}  do
     resource :registration do
       member do
         get 'register', :to => 'registration#register', :as => :register
         get 'entry', :to => 'registration#enrty', :as => :entry
       end     
     end
   end
end

Upvotes: 4

Related Questions