Here is my problem. Let's say my domain consists of two models, Factories and Workers. Now when a user goes to create a Worker in the _form view, they have to select a Factory. To do this, a list of Factories based on the state of the worker is provided and the content of the dropdown is changed through JS whenever the state of the worker changes. This works like a charm.

However, when going to edit a user I seem to be stuck as the accepted way to populate and auto-select the worker's factory. It seems like for the edit view, the select tag with options_for_select specified is the way to go. Unfortunately, this breaks my _form view when adding a new Worker because they don't have a state with which I can populate the collection.

TL;DR I can populate a dropdown in the new _form view using JS, the same paradigm doesn't work with the edit _form as I have to pre-populate the dropdown based on the current Factory and state of the Worker.

Any help is greatly appreciated.

EDIT: The Factory dropdown is populated by an ajax query which passes the selected state to the controller to find out which Factories are in that state.

I'm a Ruby noob, so this all isn't expected to work off the bat, I am just trying to reconcile the difference in data-population of the dropdown between the create view and the edit view. I may end up just creating two _forms.

1 Answer
1

I understand that the problem is that you get an error in when the form for worker is rendered when creating a new worker because the new worker doesn't have a state that you can pre-populate the dropdown based on.

If that's the problem then you could use @worker.new_record? to test if the @worker is a new record or not, if so then you could render the factory select box with no options except a prompt that tells the user to choose a state first.

That was it ! My solution involved using an if-else construct in my erb file examining is the Worker had a Factory. I used a Proc to select the correct one in the options_for_something_I_can't_remember. These rails conventions are coming slowly and hard earned, but they are coming.
–
FlexFiendDec 28 '12 at 3:59