2012-10-03 45 views
8

我有以下date_select幫助程序。我想添加一個類,但它不生成HTML。向date_select添加CSS類

<%= date_select :recipient, :birthday, :order => [:day, :month, :year], :start_year => 1920, :end_year => 2013, :class => "input-mini" %> 

我也有一個哈希嘗試了一些解決方案建議,但我得到一個語法錯誤:

<%= date_select :recipient, :birthday, :order => [:day, :month, :year], :start_year => 1920, :end_year => 2013, {:class => "input-mini"} %> 

不知道我真的能理解何時以及如何將其與哈希結構。

回答

8

這應該工作:

<%= date_select :recipient, :birthday, 
:order => [:day, :month, :year], 
:start_year => 1920, 
:end_year => 2013, 
:html=>{:class => "input-mini"} 
%> 

更新:對於Rails的5

<%= date_select :recipient, :birthday, 
       { 
       :order => [:day, :month, :year], 
       :start_year => 1920, 
       :end_year => 2013 
       }, 
       {:class => "input-mini"} %> 
0

嘗試

HTML散列內CLAS像:html=>{:class=>'input-mini'}

<%= date_select :recipient, :birthday, :order => [:day, :month, :year], :start_year => 1920, :end_year => 2013, :html=>{:class => "input-mini"} %> 
28

經驗證的答案並沒有爲我工作,什麼工作是:

<%= date_select 
    :recipient, 
    :birthday, 
    {:order => [:day, :month, :year], :start_year => 1920, :end_year => 2013}, 
    {:class => "input-mini"} 
%> 

哪個更有意義根據docs

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

正如我們在​​看到,date_select幫手希望具體參數:

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

對我來說只是不要加方法OD選擇和留下的選項哈希{}空完美工作:

datetime_select(:recipient, {}, {:class => 'custom-select'}) 

或者,如果你想與:birthday參數去使用,你可以簡單地使用:

datetime_select(:recipient, :birthday, {}, {:class => 'custom-select'})