我第一次看到这种行为。在制作插入物时,插入物缺少其中一个参数。这是我的模型和控制器
模型
class Campaign < ApplicationRecord
belongs_to :supplier
has_many :campaign_offices
has_many :campaign_plans
has_many :campaign_plan_tiers
end
控制器
class CampaignsController < ApplicationController
before_action :get_model, :only => [ :edit, :update, :destroy, :show ]
def index
@models = Campaign.order(:display_name)
end
def show
end
def create
Campaign.create(model_params)
redirect_to campaigns_path
end
def edit
@models = Campaign.order(:display_name)
end
def update
@model.update(model_params)
redirect_to campaigns_path
end
def destroy
@model.destroy
redirect_to campaigns_path
end
private
def get_model
@model = Campaign.find(params[:id])
end
def model_params
params.require(:campaign).permit( :display_name, :supplier_id, :campaign_code, :start_date, :end_date )
end
end
这就是插件的工作方式。这是我本地开发计算机上的服务器日志的复制粘贴
Started POST "/campaigns" for 127.0.0.1 at 2018-04-04 13:19:44 -0400
(0.4ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
(0.2ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
Processing by CampaignsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"PA+xaH7ehMCNFB7+FfQ38KF2W07T7rGebj3rBQhJ6SLKqxyJTyTrES0qKDE5HWhEZ1x0p9vTO4Ur1qk4CYvPgQ==", "campaign"=>{"display_name"=>"Beat the heat", "campaign_code"=>"BTH-0293949", "supplier_id"=>"6", "start_date"=>"05/01/2018", "end_date"=>"05/31/2018"}, "commit"=>"Submit"}
(0.2ms) BEGIN
Supplier Load (0.3ms) SELECT `suppliers`.* FROM `suppliers` WHERE `suppliers`.`id` = 6 LIMIT 1
**SQL (0.7ms) INSERT INTO `campaigns` (`display_name`, `supplier_id`, `campaign_code`, `start_date`, `created_at`, `updated_at`) VALUES ('Beat the heat', 6, 'BTH-0293949', '2018-01-05', '2018-04-04 17:19:44', '2018-04-04 17:19:44')**
(0.8ms) COMMIT
Redirected to http://localhost:3000/campaigns
Completed 302 Found in 58ms (ActiveRecord: 3.9ms)
结束日期不会在insert或update语句中出现。它被省略了。该列存在于db中,迁移也列出了列。我也启动了服务器,但同样的事情。模型中没有回调,以及上面粘贴的模型代码也很明显。
环境是mysql,rails 5.1.6,ruby 2.4.0
我以前从未见过这个。任何帮助,将不胜感激。
谢谢
答案 0 :(得分:2)
如果查看start_date
的参数,我们会看到:
"start_date"=>"05/01/2018"
在INSERT语句中,我们看到ISO-8601格式的start_date
:
'2018-01-05'
输入格式被解释为DD/MM/YYYY
。如果我们查看end_date
,我们会看到:
"end_date"=>"05/31/2018"
MM/DD/YYYY
而不是DD/MM/YYYY
。当您的模型尝试使用DD/MM/YYYY
格式将其解析为日期时,它会变得无意义并且会忽略日期。
最好的办法是在显示范围内使用ISO 8601日期格式(YYYY-MM-DD
)。修复客户端中的格式,或者在必要时在控制器中修复格式。