twitter-bootstrap 将 bootstrap-datepicker-rails 添加到表单中的 date_select

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14712281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 16:16:26  来源:igfitidea点击:

Adding bootstrap-datepicker-rails to date_select in form

ruby-on-railsruby-on-rails-3formstwitter-bootstrapdatepicker

提问by holyredbeard

So, I have a form that uses 'date-select', which renders three select boxes per used date-select (year, month and day).

所以,我有一个使用“日期选择”的表单,它为每个使用的日期选择(年、月和日)呈现三个选择框。

Instead I want to use datepicker, and I've followed the instructions here. However, I don't have a clue how to actually implement the datepicker in the form in the view.

相反,我想使用 datepicker,并且我已按照此处的说明进行操作。但是,我不知道如何在视图中的表单中实际实现日期选择器。

I'm using the following line in application.js...

我在 application.js 中使用以下行...

$('.datepicker').datepicker()

...so I guess I need to give the select boxes the class .datepicker?

...所以我想我需要给选择框类.datepicker?

<%= form_for @project do |f| %>
    <div class="text_field">
        <%= f.label :title%>
        <%= f.text_field :title%>
    </div>
    <div class="text_field">
        <%= f.label :description%>
        <%= f.text_field :description%>
    </div>
    <div class="dropdown">
        <%= f.label :start_date%>
        <%= date_select :start_date, :startdate %>
    </div>
    <div class="dropdown">
        <%= f.label :end_date%>
        <%= date_select :end_date, :enddate%>
    </div>
    <div class="select">
        <%= f.label :title%>
    </div>
    <div class="submit">
        <%= f.submit "Spara" %>
    </div>
<% end %>

回答by R Milushev

Try :

尝试 :

<%= date_select :start_date, :startdate , :class => 'datepicker' %>

EDIT: I have replicated your code on my machine and found the possible mistakes:

编辑:我已经在我的机器上复制了你的代码并发现了可能的错误:

This gem selects the date in a text field, not in a date-select. The input should be text_fieldand it looks like this:

这个 gem 在文本字段中选择日期,而不是在date-select. 输入应该是text_field这样的:

<%= f.text_field :date, :class => 'datepicker' %>

As described in the gem's install guide , you should be sure to requireit both in your application.cssand application.js. One more important thing : in your projects.js.coffeemake sure that the DOM is loaded , like this :

按创业板的描述安装指南,你应该确保require它无论是在你application.cssapplication.js。更重要的一件事:projects.js.coffee确保 DOM 已加载,如下所示:

jQuery ->
  $('.datepicker').datepicker()