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)

Do I need to provide more information for someone to look into this and
help me?

On Fri, Aug 28, 2009 at 11:28 PM, Quee
Mm[email protected] wrote:

Do I need to provide more information for someone to look into this and
help me?

“more information”? I didn’t see any information – where’s the code
you’ve tried that isn’t producing the result you want?


Hassan S. ------------------------ [email protected]
twitter: @hassan

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 S…

Jonathan
Swifthttp://www.brainyquote.com/quotes/authors/j/jonathan_swift.html

  • “May you live every day of your life.”

On Sat, Aug 29, 2009 at 1:39 PM, Quee Mm

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”);
}
});

}
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");
}

});

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.

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.

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

On Sat, Aug 29, 2009 at 1:09 AM, Quee
Mm[email protected] wrote:

<%= 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.


Hassan S. ------------------------ [email protected]
twitter: @hassan

On Sat, Aug 29, 2009 at 9:24 AM, Quee
Mm[email protected] wrote:

… 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.


Hassan S. ------------------------ [email protected]
twitter: @hassan

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

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 S…

Joan
Crawfordhttp://www.brainyquote.com/quotes/authors/j/joan_crawford.html

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

On Sat, Aug 29, 2009 at 9:54 PM, Quee Mm