the :controller key works fine, but :type is just ignored. This would
be very handy for an STI-backed controller I have, to change a url
like "/bar/new?type=foo" to "/foo/new" but still handle it with
BarController.
but no answers. I've tried a bunch of stuff
with :requirements, :as, :path_prefix, and nesting, but all to no
avail. If anyone has any advice, I'd be most grateful.
Ian-
It might be helpful if you gave a few more examples of what you want
to accomplish. Are you trying to have the following routes all point
to the "new" method on the same controller?
Sure. You have it basically right, but let me elaborate.
I have models like so:
class Papa < ActiveRecord::Base; abstract_class = true; end
class Daughter < Papa; end
class Son < Papa; end
I have this controller:
class PapaController < ApplicationController
def new; @model = new_model; end
def new_model
params[:type].classify.constantize.new # there's more safety logic
here in reality, but this is the gist
end
end
I started with the default route:
map.resources :papa
So, then, I can create a new son by doing GET /papa/new?type=son,
which is fine, but not ideal. Ideally, I would do GET /son/new, but it
would still use the Papa controller, and set params[:type] to "son".
This is what I have right now:
map.resources :papa, :path_prefix => '/:type'
which allows me to GET /son/papa/new, and create links like
papa_path(@model.type.to_s.downcase, @model).
This will point both routes to the papas controller. You won't see "sons" or "daughters" come through as params. But you can query request.env['REQUEST_URI'] and you would get back something like '/sons/new/'.
So:
1. request.env['REQUEST_URI'].split("/") will give you an array ["","sons","new"]
2. access [1] of the array and you will have your parameter.
A little convoluted but I think that it would do what you want.
Sigh, yeah, there's always that way. So unrailsy... but probably what
I'll end up doing. Or I could patch Resources to make it push any
unclaimed options into params. But parsing the URI by hand is probably
easier.