3

Background: My goal is for a view to display a list of "condition" has_many objects, which are themselves STI subclasses of a StateDescription. I want a user to be able to pick what type of state description they are from a drop down menu (which will conditionally display a different type of form, eventually)

Inside of my main forms, I am doing a nested form like this:

  <%= f.fields_for :conditions do |e| %>   
    <li>
        <%= e.select(:type, StateDescription.subclasses.collect{|x| x.to_s}, options_for_select(StateDescription.subclassSelectForms)) %>
        <br>
        <%= e.label :title %>
        <%= e.text_field :title %>
    </li>
  <% end %>

This works just fine with the text field at the bottom there (I can change values and save them, etc). But when I try to do a select statement, it explodes.

Specifically, if I don't use e.select and just do:

<%= select(:type, StateDescription.subclasses.collect{|x| x.to_s}, options_for_select(StateDescription.subclassSelectForms)) %>

it renders just fine, but doesn't actually change any values as it is not associated with a model.

If I get rid of trying to have it display a value different than it submits and just do

<%= e.select(:type, StateDescription.subclasses.collect{|x| x.to_s}) %>

Then it works just fine(I can submit, the value is saved to the database, I can retrieve it, etc).

I can LEAVE it like this, but I had wanted to have my more human readable string display instead of a rails class thing (which might look like gibberish to a non-programmer end user).

So, my question is: Why do options_for_select break when nested and associated with a form, but dont' seem to when not associated with a form? Is it just a known bug? Am I doing something wrong?

Edit:

.subclasses is a standard rails calls which returns a list of subclasses for an object.

.subclassSelect forms goes through each subclass, and turns it into a hash of the form:

{subclass.to_s => subclass.human_readable_string} and compiles all of them into an array.

I know this array of hashes works, because if I do NOT put "e" on the front, it displays correctly and works "right" for what I have done (i.e. the subclass is returned correctly based on which human readable string I select from the drop down menu, but since it's not actually associated with any form, it doesn't get set in the controller).

J.R.
  • 5,789
  • 11
  • 55
  • 78

0 Answers0