views:

27

answers:

0

Hello, I'm using STI in Rails, and I've got a Vehicle object, that has many different types of subclasses, like Car, Truck, etc. It's for a simple app, so STI works fine in this case, but I'm having trouble creating a single form where any type of Vehicle record can be created.

Using the following routing:

resources :vehicles
resources :cars, :controller => 'vehicles'
resources :trucks, :controller => 'vehicles'

I can have /cars and /trucks routing set up, and both pointing to the same form. However, since the form is pointing to the vehicles controller, and generating a Vehicle object for the form, it has no way to know that the /cars url should create a Car object.

I'm trying to get a routing system set up where /cars would point to a form that would intrinsically know to make a object for the form using either Car.new or even Report.new(:type => "Car"). I thought about working a routing system like /vehicles/:subclass, and somehow using params[:subclass] in the controller, but I also can't figure out how to do that sort of routing and still avoid other routing errors caused by Rails' STI magic.

I could always parse the URL to get the value, but that seems like an unsafe and hacky way to go about it.

I'm curious if anyone has any advice or experience on the Rails way to do this. Thanks!