我正在尝试设计一个表单,让用户设置他们购物的日期。此表单将创建“列表”模型并定义“日期”属性。我创建了这个列表模型:
$ rails generate model List name:string date:date user_id:integer
现在我只希望我的表单设置日期。这是我的代码:
<%= form_for @list do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.date_select :date %>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
但是当我在浏览器中提交表单时,页面告诉我日期字段仍然是空白的,即使我提交的表单中选择了一个日期(我已经设置了我的列表对象以验证是否存在“日期“属性”。为什么表单无法成功提交日期并创建列表?谢谢!
1 error prohibited this list from being saved:
There were problems with the following fields:
* Date can't be blank
这是我的控制器代码:
ListsController < ApplicationController
before_filter :authenticate, :only => [:create, :destroy]
def create
@list = current_user.lists.build(params[:list])
if @list.save
flash[:success] = "Shopping List created!"
redirect_to root_path
else
render 'pages/home'
end
end
答案 0 :(得分:0)
我会建议两件事:
1)不要将字段称为“日期”。这是一个保守的词,可能会产生冲突。
2)使用像calendar_date_select(https://github.com/timcharper/calendar_date_select)这样的插件。您的用户会喜欢它。