views:

42

answers:

1

I have an edit screen for a model that's nested inside 2 other resources. To make the example easy to understand, it looks sort of like this:

Make -> Model -> Car

I have a Car edit screen that lets you edit the few fields that it contains. (assume the url is: /makes/124/models/12512/cars/1125/edit)

I want to add a cascading pair of dropdowns to allow the user to re-assign a car to a different make/model. (I'm doing this to clean up some of the bad data that's in the system).

The form looks like this:

<% form_for [@car.model.make, @car.model, @car] do |f| %>

    <%= f.error_messages %>

        <!-- this is where I want the cascading dropdowns -->   

    <p> <%= f.label :color %><br />
     <%= f.text_field :color %></p>

    <p> <%= f.label :mileage %><br />
     <%= f.text_field :mileage %></p>

    <p><%= f.submit "Update Car" %></p>
<% end %>

I can add @makes to this view from my controller, and I know how to make the select box, but what I don't understand is how I can have the value automatically selected when the form loads.

What's the correct way to build such a form, with fields for some of the parent resources?

A: 

You should use :shallow => true as described here so that your edit URI is http://host:port/cars/1125/edit

Chandra Patni
Actually I have friendly-looking routes, so the full URL has meaning, though I'd agree with you if I was just using the id for each one. (Something like /makes/52-honda/12-accord/ .....) - The other reason is that the URL is "hackable" meaning you can take a segment off and it still works.
Ben Scheirman