我有一个带有日期字段的小表单,我正在尝试将其保存到数据库中。除了日期选择器中的日期字段之外,所有其他字段都将被保存。下面是 View 和 Controller
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
显示 View
<%= 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。因此,您无法在“真实”提醒
上设置值。从模型中删除 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/