The rails date_select class does not take effect

I have use date_select ..

<%= f.date_select :birthday, :order => [:month, :day], :prompt => { :day => 'Select day', :month => 'Select month' }, :html => {:class => "select birthday"} %> 

But the class does not appear in html ..

 <select id="profile_birthday_2i" name="profile[birthday(2i)]"> <select id="profile_birthday_3i" name="profile[birthday(3i)]"> 

I tried too ..

 <%= f.date_select :birthday, :order => [:month, :day], :prompt => { :day => 'Select day', :month => 'Select month' }, :class => "select birthday" %> 

That didn't work either. Any ideas?

+7
source share
2 answers

HTML parameters are the fourth argument to the date_select method, not the key in the third argument.

In the documentation:

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

So you need to:

 f.date_select :birthday, { :order => [:month, :day], :prompt => { :day => 'Select day', :month => 'Select month' } }, {:class => "select birthday"} 
+12
source

To specify a class, you need to use html_options , not html .

I believe this will work, although I have not tested it.

 <%= f.date_select :birthday, :order => [:month, :day], :prompt => { :day => 'Select day', :month => 'Select month' }, :html_options => {:class => "select birthday"} %> 

See API description here:

http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html

Note. The docs say:

If something is passed in the hash file html_options, it will be applied to each select tag in the set.

Therefore, make sure you expect the class to be displayed for each element.

+1
source

All Articles