“Local” or a value between :year_made and Time.now.year
:year_registered #If :year_imported == “Local”
a value between :year_made and Time.now.year
#else
a value between :year_imported and Time.now.year
#end
I am trying to build drop downs which follow these rules and am running
into difficulties. Any help in building these where they work both in
new and edit action (with the correct value as saved in db preselected
will be greatly appreciated)
man i have so many different combos but here is my latest code.
In my view I have
<%= f.select(:year_made, ((1980…Time.now.year).collect {|p| [ p, p
]}).reverse, {:prompt=>“Select a Year”}) %>
<%= f.select(:year_imported, ((1980…Time.now.year).collect {|p| [ p, p
]}).reverse, :prompt=>“Select a Year”) %>
<%= f.select(:year_registered, ((1980…Time.now.year).collect {|p| [ p,
p ]}).reverse, :prompt=>“Select a Year”) %>
then i am using JavaScript to control what the drop downs will show…
jQuery(’#vehicle_year_made’).change(function(){
year_made_id = jQuery("#vehicle_year_made :selected").val();
if (year_made_id != “” ) {
var currentTime = new Date()
var options = ‘Select a YearLocal’;
for (i=currentTime.getFullYear(); i >= year_made_id; i–) {
options += ‘’ + i + ‘’;
}
jQuery("#vehicle_year_imported").html(options);
jQuery("#vehicle_year_imported").removeAttr(“disabled”);
jQuery('#vehicle_year_imported').change(function(){
if ( jQuery("#vehicle_year_imported :selected").val() == "" ||
jQuery("#vehicle_year_imported :selected").val() == “Select a Year”)
{
jQuery("#vehicle_year_registered").attr(“disabled”,
“disabled”);
}
year_import_id = jQuery("#vehicle_year_imported
:selected").val();
if (year_import_id != “Select a Year” && year_import_id !=
“Local”) {
var options = ‘Select a YearUn-Registered’;
for (i=currentTime.getFullYear(); i >= year_import_id; i–) {
options += ‘’ + i + ‘’;
}
jQuery("#vehicle_year_registered").html(options);
if (options != ‘Select a Year’) {
jQuery("#vehicle_year_registered").removeAttr(“disabled”);
}
}
else if (year_import_id == “Local”) {
var options = ‘Select a YearUn-Registered’;
for (i=currentTime.getFullYear(); i >= year_made_id; i–) {
options += ‘’ + i + ‘’;
}
jQuery("#vehicle_year_registered").html(options);
if (options != ‘Select a Year’) {
jQuery("#vehicle_year_registered").removeAttr(“disabled”);
}
}
else
{
options = ‘Select a Year’;
jQuery("#vehicle_year_registered").html(options);
jQuery("#vehicle_year_registered").attr(“disabled”,
“disabled”);
}
});
The code works fine and does generate the three drop downs i think I am
missing the point where I can tell the code what the already :selected
value is.
As i already said, when i do a new record, the code and the javascript
work just fine and give me the results that i need, now the edit is a
different story, for that all i get is three drop downs with 1980 - 2009
as options in all three fields and it seems as if the javascript is not
present.
<%= f.select(:year_made, ((1980..Time.now.year).collect {|p| [ p, p
]}).reverse, {:prompt=>"Select a Year"}) %>
<%= f.select(:year_imported, ((1980..Time.now.year).collect {|p| [ p, p
]}).reverse, :prompt=>"Select a Year") %>
<%= f.select(:year_registered, ((1980..Time.now.year).collect {|p| [ p,
p ]}).reverse, :prompt=>"Select a Year") %>
If you look at this with syntax highlighting, like in your pastie,
you’ll see
there’s a difference in the code for the first vs second and third. May
not
be the issue but…
then i am using JavaScript to control what the drop downs will show…
I would first disable JavaScript and make sure the selects are being
generated with the initial values you want.
Then, if this is strictly a JS issue (and it fails in Firefox) you can
use
Firebug to troubleshoot the problem.
I would like to tell you one small thing.
You don’t need :selected in any of the val() calls.
for a select box, the val() method is wise enough to return the selected
value.