alexs333
alexs333

Reputation: 12553

Rails 3 Nested Routing

I have very interesting scenario:

I've specified two controllers, one for global events and another another once for company specific events. In routes, it is specified like this:

resources :companies do
  resources :events
end
resources: events

Running rake routes I can see the routes being generated:

events GET  /events(.:format) events#index
company_events GET /companies/:company_id/events(.:format) events#index 

Both paths seem to route to the same controller (the global one)... I have the second controller under controller/companies that goes something like this:

class Companies::EventsController < ApplicationController
 # stuff
end

It never routes in that controller above, no matter whether I use company_evens_path(@company). always goes to the other one. It used to work in rails 2.3 for me, I'm currently using 3.2

Upvotes: 0

Views: 260

Answers (1)

Alex Marchant
Alex Marchant

Reputation: 2510

Ok as stated above, I would recommend doing something like this:

def index
  if params[:company_id]
    @events = Company.find(params[:company_id]).events
  else
    @events = Events.all
  end
end

although if you need to you can specify a controller:

resources :companies do
  resources :events, :controller => "companies/events"
end
resources: events

and just create a companies folder inside your controllers folder to put your Companies::EventsController inside

Upvotes: 2

Related Questions