Ruby-on-rails 如何在 rails 中使用带有 form_for 的日期选择器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30522655/
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-09-03 00:14:55  来源:igfitidea点击:

How to use date picker with form_for in rails?

ruby-on-railsbootstrap-datepicker

提问by TheFirefly

I'm new to rails,Any clues ? and it would be better if you can suggest me what to read as well.

我是 Rails 的新手,有什么线索吗?如果你也能建议我读什么书就更好了。

Im adding a date picker plugin into an existing rails4 app. i found this pluginon Github. i followed the instruction and configured everything in my app. Visiting http://localhost:3000/bookings/newlooks fine, but after i fill up the information and submit it , error occurs :

我在现有的 rails4 应用程序中添加了一个日期选择器插件。我在 Github 上找到了这个插件。我按照说明在我的应用程序中配置了所有内容。访问http://localhost:3000/bookings/new看起来不错,但在我填写信息并提交后,出现错误:

ArgumentError in BookingsController#create
First argument in form cannot contain nil or be empty

booking_controller.rb:

预订控制器.rb:

def new
    @booking = Booking.new
  end

  def create
    @booking = current_user.bookings.build(booking_params)    # Not the final implementation!
    if @booking.save
      flash[:success] = "You have submited the information successfully!"
      redirect_to root_url
    else
      render 'static_pages/home'
    end
  end

new.html.rb

新的.html.rb

    <%= form_for (@booking) do |f| %>
    <%= render 'shared/errorbooking_messages' %>
      <%= f.label :date_of_tour %>
      <input data-provide="datepicker">

      <%= f.label :hotel_name %>
      <%= f.text_field :hotel_name, class: 'form-control' %>

      <%= f.label :hotel_address %>
      <%= f.text_field :hotel_address, class: 'form-control' %>

回答by Akshay Borade

Replace this input field

替换此输入字段

<input data-provide="datepicker">

by

经过

<%= f.text_field :date_of_tour, "data-provide" => 'datepicker' %>

Hope it's helpful.

希望它有帮助。

Just to build on this answer. As of rails 4 you can also use the following format.

只是为了建立在这个答案上。从 rails 4 开始,您还可以使用以下格式。

<%= f.text_field :date_of_tour, data:{ provide:'datepicker' } %>