Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails date_select class won't take effect

I have the following use of 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 show up in the html..

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

I also tried..

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

That did not work either. Any ideas?

like image 208
absolutskyy Avatar asked Feb 27 '13 19:02

absolutskyy


2 Answers

The HTML options are a fourth argument to the date_select method, rather than being a key in the third argument.

From the documentation:

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

So you'd want:

f.date_select :birthday, { :order => [:month, :day], :prompt => { :day => 'Select day', :month => 'Select month' } }, {:class => "select birthday"} 
like image 146
Douglas F Shearer Avatar answered Nov 12 '22 19:11

Douglas F Shearer


You need to use html_options, not html to specify the class.

I believe this will work, though I've not tested it.

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

See the API description here:

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

Note: The docs say:

If anything is passed in the html_options hash it will be applied to every select tag in the set.

So make sure you expect that class to show up on each element.

like image 23
Kevin Bedell Avatar answered Nov 12 '22 17:11

Kevin Bedell