将一个CSS类添加到date_select

时间:2012-10-03 18:25:17

标签: css ruby-on-rails

我有以下date_select助手。我想添加一个类,但它不生成HTML。

<%= 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"} %>

不确定我是否真的了解何时以及如何使用哈希格式化它。

4 个答案:

答案 0 :(得分:29)

经验证的答案对我不起作用,有效的是:

<%= 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 = {})

答案 1 :(得分:10)

这应该有效:

<%= 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"}  %>

答案 2 :(得分:0)

在像:html=>{:class=>'input-mini'}

这样的html哈希中进行分类
<%= date_select :recipient, :birthday, :order => [:day, :month, :year], :start_year => 1920, :end_year => 2013, :html=>{:class => "input-mini"} %>

答案 3 :(得分:0)

正如我们在docs上看到的那样,date_select帮助器需要特定的参数:

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

对我来说,不要添加方法选项,并将选项hash {}空置完美:

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

或者,如果您想使用:birthday param,您只需使用:

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