I’m trying to implement a selectbox in a view that is populated with data from another model (called comptes). Here is the code of the view:
<%= f.select(:debit, @in.collect {|p| [p.nom, p.numero]} + ['Ajouter compte'], {:include_blank => 'Sur quel compte?'})%>
What I’m trying to do is: when the user select the last option (‘Ajouter compte’), I want the system to open a new window and go to the new_compte_path.
I think Jquery could help but I don’t know how to do it.
I’ve done something similar in the past. You’ll need an event handler to detect when that option gets selected; when it is, open up a modal where you can put in the details for the new company. When the user hits OK, add the company via AJAX, and if the add was successful, add that item to your options list and select it.
To do this, you’ll need an ID on your “Add Company” option so jQuery can find it, and then an event handler to deal with it getting selected. I think I used “option/onclick”, but “select/changed” is probably more correct (Might not work in IE, though! IE often won’t send “changed” events until a field loses focus.).
Hope this helps!