cgr
cgr

Reputation: 1121

Proper Routing in Rails for a diverse set of tools

I am trying to clean up my routes and I have a situation I am not sure what to do with:

I have a good number of tools & reports spread across a variety of controllers which are basicially collections but they all revolve around 2 parameters: divisions and term.

I wanted to do something like this:

resources :divisions do
  resources :terms do
    scope "/tools" do
      #something to redirect to controller "carriers" and action "carrier_distribution"
      #something to redirect to controller "courses" and action "secour monitor"
    end
  end
end

Most of the resources I am interested in are nested within division.

It would be very clean & helpful to have a URL like this:

/divisions/1/terms/1/tools/carrier_distribution

I do have the option to make a "tools" controller which could kick this stuff off. I am just not sure what is appropriate (like if i should stick to passing a term parameter).

thanks!

Upvotes: 0

Views: 54

Answers (1)

jemminger
jemminger

Reputation: 5173

Personally I'd go with the ToolsController, and map in your routes:

map.connect "/divisions/:division_id/terms/:term_id/tools/:action", :controller => "tools"

Then your ToolsController can just access those params:

class ToolsController < ApplicationController
  def report1
    render :text => "hello from report1, division[#{params[:division_id]}] term [#{params[:term_id]}]"
  end

  def report2
    render :text => "hello from report2, division[#{params[:division_id]}] term [#{params[:term_id]}]"
  end
end

Upvotes: 1

Related Questions