12

我有以下 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

4 回答 4

31

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

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

根据文档,这更有意义:

date_select(object_name, method, options = {}, html_options = {})
于 2013-05-31T16:08:22.720 回答
11

这应该有效:

<%= 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"}  %>
于 2012-11-13T18:46:41.727 回答
1

正如我们在文档中看到的,date_select助手需要特定的参数:

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

对我来说,只是不要添加方法选项并将选项散列{}留空,效果很好:

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

或者,如果您想与:birthdayparam 一起使用,您可以简单地使用:

datetime_select(:recipient, :birthday, {}, {:class => 'custom-select'})
于 2017-04-12T00:17:23.250 回答
0

尝试

html哈希中的类 :html=>{:class=>'input-mini'}

<%= date_select :recipient, :birthday, :order => [:day, :month, :year], :start_year => 1920, :end_year => 2013, :html=>{:class => "input-mini"} %>
于 2012-10-03T18:29:01.487 回答