2012-10-03 44 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'})