我有一个小表格,其中有日期字段,我试图保存到数据库中。除了datepicker中的日期字段外,所有其他字段都被保存。下面是视图和控制器
def addreminder
@user = set_user
@userremind=UserReminder.new do |u|
u.user_id=params[:id]
u.car_id=params[:car]
u.reminder=Time.local(params[:reminder_date]).strftime('%Y/%d/%m')
u.service_type=params[:service_type]
u.active_status=0
end
显示视图
<%= form_tag("/users/addreminder/#{current_user.id}",:method=>"post",:id => 'add_reminder') do %>
<%= select_tag(:car, options_for_select(@cars.collect{|u|[u['car_name'],u['id']]}),
{class: "span3 input-md main_select"}) %>
<%= label :service_type,"Select Service Type"%>
<%= select_tag(:service_type, options_for_select(@services.collect{|u|[u['service_type'],u['service_type']]}),
{class: "span3 input-md main_select"}) %>
<%= (text_field_tag :reminder_date,nil,placeholder: "Select Reminder Date",data: {provide: "datepicker"})%>
<%= submit_tag "Remind me",:class=>"btn btn-xlarge"%>
<% end %>
<script>
$(document).ready(function(){
$('.datepicker').datepicker();
});
</script>
这是日志参数
Parameters: {"utf8"=>"✓""authenticity_token"=>"7wSEVk24GAgpMtIYoEsa195DYsqB4UsOQ6BoAMzgOfc=", "car"=>"27", "service_type"=>"Oil Change", "reminder_date"=>"07/02/2015", "commit"=>"Remind me", "id"=>"5"}
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 5 ORDER BY `users`.`id` ASC LIMIT 1
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = ? LIMIT 1 [["id", "5"]]
SQL (0.1ms) BEGIN
SQL (0.4ms) INSERT INTO `user_reminders` (`active_status`, `car_id`, `created_at`, `service_type`, `updated_at`, `user_id`) VALUES (?, ?, ?, ?, ?, ?) [["active_status", 0], ["car_id", 27], ["created_at", Fri, 26 Jun 2015 19:52:10 UTC +00:00], ["service_type", "Oil Change"], ["updated_at", Fri, 26 Jun 2015 19:52:10 UTC +00:00], ["user_id", 5]]
(32.8ms) COMMIT
数据库模式:
CREATE TABLE user_reminders (
id int(11) NOT NULL AUTO_INCREMENT, user_id int(11) DEFAULT NULL,
car_id int(11) DEFAULT NULL,
service_type varchar(255) DEFAULT NULL,
reminder varchar(25) DEFAULT NULL,
active_status int(1) NOT NULL DEFAULT '1',
created_at datetime DEFAULT NULL,
updated_at datetime DEFAULT NULL,
PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=56 DEFAULT CHARSET=utf8
该模型:
class UserReminder < ActiveRecord::Base
belongs_to :user
attr_accessor :reminder
validates :reminder, presence: true
# validate :reminder_date_cannot_be_in_the_past
def reminder_date_cannot_be_in_the_past
errors.add(:reminder, "Can't be in the past") if reminder_date <= Date.today.strftime('%m/%d/%Y')
end
end
最佳答案
有几个问题与此不起作用:
第一:attr_accessor :reminder
行覆盖Rails已经为reminder
列提供的setter和getter。结果,您将无法在“真实” reminder
上设置值。从模型中删除attr_accessor :reminder
行。
第二:Time.local
无法从参数解析字符串。
Time.local('07/02/2015').strftime('%Y/%d/%m')
#=> "0007/01/01"
使用
Time.parse
代替:Time.parse('07/02/2015').strftime('%Y/%d/%m')
#=> "2015/07/02"
第三:您的自定义验证器尝试比较两个不同的事物。
reminder_date
不存在,并且Date.today.strftime('%m/%d/%Y')
的格式不同于文本列中保存的格式。将该方法更改为:def reminder_date_cannot_be_in_the_past
if reminder <= Date.today.strftime('%Y/%d/%m')
errors.add(:reminder, "can't be in the past")
end
end
此外,还需要考虑以下几点:
IMO将
reminder
列的类型更改为DATE
是有意义的。这将使代码容易得多,因为您随后可以比较Date
对象,并且需要strftime
方法调用。在我看来,您好像不处理验证错误。处理
save
调用的结果并在失败时将错误返回给用户可能是有意义的。关于javascript - Bootstrap-Date选择器日期字段未保存到mysql数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31083157/