Help with Select

I have the following three items in the db:

:year_made # a value between 1980 and Time.now.year

:year_imported # "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)

"more information"? I didn't see *any* information -- where's the code you've tried that isn't producing the result you want?

man i have so many different combos but here is my latest code.

In my view I have <code> <%= 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") %> </code> then i am using JavaScript to control what the drop downs will show.. <code>   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 = '<option>Select a Year</option><option value="Local">Local</option>';       for (i=currentTime.getFullYear(); i >= year_made_id; i--) {         options += '<option value="' + i + '">' + i + '</option>';       }       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 = '<option>Select a Year</option><option value="Un-Registered">Un-Registered</option>';           for (i=currentTime.getFullYear(); i >= year_import_id; i--) {             options += '<option value="' + i + '">' + i + '</option>';           }           jQuery("#vehicle_year_registered").html(options);           if (options != '<option>Select a Year</option>') {             jQuery("#vehicle_year_registered").removeAttr("disabled");           }         }         else if (year_import_id == "Local") {           var options = '<option>Select a Year</option><option value="Un-Registered">Un-Registered</option>';           for (i=currentTime.getFullYear(); i >= year_made_id; i--) {             options += '<option value="' + i + '">' + i + '</option>';           }           jQuery("#vehicle_year_registered").html(options);           if (options != '<option>Select a Year</option>') {             jQuery("#vehicle_year_registered").removeAttr("disabled");           }         }         else         {           options = '<option>Select a Year</option>';           jQuery("#vehicle_year_registered").html(options);           jQuery("#vehicle_year_registered").attr("disabled", "disabled");         }       });

    }     else     {       options = '<option>Select a Year</option>';       jQuery("#vehicle_year_imported").html(options);       jQuery("#vehicle_year_imported").attr("disabled", "disabled");       jQuery("#vehicle_year_registered").html(options);       jQuery("#vehicle_year_registered").attr("disabled", "disabled");     }   }); </code>

This works perfectly fine at new but when i edit a record the JavaScript does not seem to work and all drop downs show 2009 - 1980 as options.

Hey can you please use some service like pastie.org to paste the code and give us the link ? Reading this code gives me a headache …

Thanks & Regards, Dhruva Sagar.

Jonathan Swift - “May you live every day of your life.”

Well this is my first time working with pastie and all so please bear with me.

Here is the link to the pastie.

http://pastie.org/private/uaaeocr2866tke49sg

thanks for looking into this.

Regards,

Quee

<code> <%= 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") %> </code>

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.

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.

..which usually indicates a JS error; are you using Firefox with Firebug?

If not, why not? Or at least wrap your code in a try/catch block and alert any error.

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.

Thanks & Regards, Dhruva Sagar.

Joan Crawford - “I, Joan Crawford, I believe in the dollar. Everything I earn, I spend.”

Thanks guys for the pointers, am working on them to see if something comes up.