如何在 rails 中使用 form_for 的日期选择器?

How to use date picker with form_for in rails?

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

我正在将日期选择器插件添加到现有的 rails4 应用程序中。我发现 this plugin on Github. i followed the instruction and configured everything in my app. Visiting http://localhost:3000/bookings/new 看起来不错,但是我填写信息并提交后,出现错误:

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

booking_controller.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

    <%= 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' %>

替换此输入字段

<input data-provide="datepicker">

来自

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

希望对您有所帮助。

仅以此答案为基础。从 rails 4 开始,您还可以使用以下格式。

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