mardi 3 mars 2015

Dynamic select field without model associations

I have a form in my rails application that has two select fields. Based on the selection in the first input, I want a different set of options to be shown in the second select field. I don't want to have to build models for these objects because my rails app is mainly a series of controllers pushing to an external database.


Here is a mock solution. I need to figure out the exact method to use and how to store the variables in my view or controller.



# First select field
<%= f.select :transportation, options_for_select([["car", "car"], ["bus, "bus]]%>

# If car is selected I would like to only show car brands in the second select field (can be in view or controller)

<% car_brands = audi, bmw, ford %>
<% bus_brands = wayfarer, bombardier %>
<%= f.select :brands, options_from_collection_for_select(car_brands) %>

Aucun commentaire:

Enregistrer un commentaire