Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 插入一个css类以选择Rails日期_Ruby On Rails_Html Helper_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails 插入一个css类以选择Rails日期

Ruby on rails 插入一个css类以选择Rails日期,ruby-on-rails,html-helper,ruby-on-rails-3.2,Ruby On Rails,Html Helper,Ruby On Rails 3.2,我正在使用date_select方法生成3个html选项,日期、月份和年份。 但是我需要每个select都有一个CSS类 我还没有找到关于如何将参数传递给助手的文档,我尝试了几种方法,但什么都没有 Rails视图: <%= f.date_select :birthday, :order => [:day, :month, :year], :start_year => 1910, :end_year => 2012, :html => {:class => ['

我正在使用date_select方法生成3个html选项,日期、月份和年份。 但是我需要每个select都有一个CSS类

我还没有找到关于如何将参数传递给助手的文档,我尝试了几种方法,但什么都没有

Rails视图:

<%= f.date_select :birthday, :order => [:day, :month, :year], :start_year => 1910, :end_year => 2012, :html => {:class => ['day', 'month', 'year']} %>
[:day,:month,:year],:start_year=>1910,:end_year=>2012,:html=>{:class=>['day','month','year']}%>
HTML输出:

<select id="poll_user_birthday_3i" name="poll_user[birthday(3i)]">
<select id="poll_user_birthday_2i" name="poll_user[birthday(2i)]">
<select id="poll_user_birthday_1i" name="poll_user[birthday(1i)]">

像我一样的HTML输出:

<select id="poll_user_birthday_3i" name="poll_user[birthday(3i)]" class="day">
<select id="poll_user_birthday_2i" name="poll_user[birthday(2i)]" class="month">
<select id="poll_user_birthday_1i" name="poll_user[birthday(1i)]" class="year">


谢谢

date\u select不提供将不同html选项传递给每个select的方法。可选的
html\u选项
参数应用于每个选项

您可以使用单独的助手
select_day
select_hour
select_minute
来编译您自己的文件


通过这种方式,您可以将单独的类传递给每个类。

根据rails文档(),方法定义如下所示

     <%= f.date_select :birthday, {:order => [:day, :month, :year], :start_year => 1910, :end_year => 2012}, {:class => 'common-class'} %>
date\u select(对象名称、方法、选项={},html\u选项={})

使用上述选项,您可以如下所示指定代码

     <%= f.date_select :birthday, {:order => [:day, :month, :year], :start_year => 1910, :end_year => 2012}, {:class => 'common-class'} %>
[:日、月、年],:开始年份=>1910,:结束年份=>2012},{:class=>common class}%>
您传递的html选项将被丢弃,因为它没有按照文档进行遵循,因此您无法在所选日期内应用任何类

您可以参考以下线程了解更多信息

此外,您还需要按照lebreeze的建议编写individual helper方法,并应用上述步骤以获得准确的输出


希望能有所帮助。

我解决这个问题的方法是使用css

看法


Rails现在确实做了OP想要的事情

:with_css_classes-如果要为“select”标记指定不同的样式,请将其设置为true。此选项自动为“选择”标记设置“年”、“月”、“日”、“小时”、“分钟”和“秒”类


谢谢。这很有帮助!“开始和结束年份”功能也非常有用。*问题在于日期选择,但推理适用于日期时间选择。*这可能是最好的
一般情况下的
答案,尽管rails仍然为datetime\u select添加了破折号和冒号,然后事情就变得一团糟了。如果您正在使用的
rails已损坏。。。我们将在css:shipit:
.datetime select:nth-child(1){
  width: 130px;
  text-indent: 15px; 
}

.datetime select:nth-child(2) {
  width: 200px;
  text-indent: 5px;
}

.datetime select:nth-child(3) {
  width: 110px;
  text-indent: 15px;
}