2015-05-29 63 views
5

我是新来的铁轨,任何线索?如果你能建议我读什么也会更好。如何在rails中使用带有form_for的日期选择器?

我添加一个日期选择器插件到现有的rails4应用程序。我在Github上发现了this plugin。我遵循指示并在我的应用程序中配置了所有内容。参观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' %> 
+0

你想jQuery的日期选择器或下拉的年,月和日期? –

+0

在你的'create'动作中,试试'@booking = Booking.new(booking_params)'。希望它有帮助。 – Zoran

+0

@ user123我实施的是日期选择器,我可以选择日期和年份,在页面上看起来很好,而不是在我提交信息之后。 – Snailwalker

回答

11

替换此输入字段

<input data-provide="datepicker"> 

通过

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

希望这是有帮助的。

只是为了建立在这个答案。至于导轨4,您还可以使用以下格式。

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

欢迎您,这2个链接,你可以参考。 https://github.com/Nerian/bootstrap-datepicker-rails https://bootstrap-datepicker.readthedocs.org/en/latest/ –

+0

我遇到过这个,我在这里遇到的可能是关于在rails中编写视图代码的知识缺乏。不管怎样,谢谢你。 – Snailwalker

+1

使用数据散列也是一个选项':data => {:provide =>:datepicker}' – mlt

相关问题