Question

We are currently in the process of upgrading our rails version and I have come across some weird error in one of my date_select tags.

Here is the culprit:

    <%= date_select :consultant_assignment, :start_date, :order => [:day, :month, :year], 
:start_year => 5.years.ago.year, :end_year => 5.years.from_now.year, :use_short_month => true %>

This throws out an error like this:

NoMethodError in Profiles#show

Showing app/views/people/_upcoming_assignments.html.erb where line #46 raised:

You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.size

Extracted source (around line #46):

43:           <label for="consultant_assignment_start_date"><%= _('Start') %>:</label>
44:           <% debugger %>
45:           
46:           <%= date_select :consultant_assignment, :start_date, :order => [:day, :month, :year], :start_year => 5.years.ago.year, :end_year => 5.years.from_now.year, :use_short_month => true %>
47:           
48:           
49:           

I have tried to trace this problem and it seems that the error is generated when I put in the :month option in the :order array. So when I do :order => [:day, :year] the page loads perfectly.

I have searched for the exact problem but being more of a front end deisgner, I'm not sure how to procede with this. Is this an error by my code? Or is it a deprecated method?

Here is the application trace:

/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:746:in `_unmemoized_month_names'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/memoizable.rb:72:in `month_names'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:781:in `month_name'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:708:in `select_month'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:705:in `upto'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:705:in `select_month'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:898:in `send'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:898:in `build_selects_from_types'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:896:in `each'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:896:in `build_selects_from_types'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:646:in `select_date'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:920:in `to_date_select_tag_without_error_wrapping'

I have googled about unmemoized month names but I found nada.

Okay, I've tried taking out the whole :order clause just to make the darn thing work. This time it doesn't throw an error but the tag doesn't appear! I tried checking in irb and apparently my date_select tag is not returning any select tags, but just a bunch of hidden tags!

I tried this with the default rails example:

date_select("post", "written_on")

irb returns this:

=> "\n\n\n"

as you can see, no select tag anywhere. I tried datetime_select and I see a select tag in there. So what's the deal with 2.3.8's date_select?

Was it helpful?

Solution 2

It seems it is a problem with i18n as rails can't see the month names declared for some reason(it SHOULD see it since the default lang is en). Actually for some reason, it doesn't see the default language. Anyway our quick fix for now was to add a MONTH_NAMES constant in constants.rb and add that to the options in date_select.

OTHER TIPS

use these format

date_select(object_name, method, options = {}, html_options = {})

put start_year , end_year , order_field etc within the options tag.

I think this might helps you.

The selects are prepared for multi-parameter assignment to an Active Record object.

Note: If the day is not included as an option but the month is, the day will be set to the 1st to ensure that all month choices are valid.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top