存在的Rails验证是错误地抛出错误

时间:2015-08-17 20:29:59

标签: ruby-on-rails ruby validation

我使用Ruby on Rails 4.x并且我有一个在线验证器,用于" rfq"的截止日期。但即使设置了截止日期,此验证程序也会抛出错误!

以下是有问题的验证(rfq.rb):

validates :due, presence:{ message:" date must be set"}

我认为这与我在同一型号的控制器中的这一行有关:

if @rfq.due.present?
  @rfq.due = Date.strptime(rfq_params[:due].to_str,"%m-%d-%Y")
  @rfq.due = @rfq.due.change(hour: 15)
end

感谢任何帮助。

编辑:添加了痕迹(我希望,这仍然是新的)

Started POST "/rfqs" for 192.168.1.29 at 2015-08-17 15:45:39 -0500
Cannot render console from 192.168.1.29! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by RfqsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"BfYwpLx5jos+lZCAnefuwgIK1UCW5Lh1xo6V72v9Hlw8JPl8igQrD8Tiuhfo0vvCYIyBdMV/PDEQqImTrvhQDg==", "rfq"=>{"customer_name"=>"Matha Laswell", "customer_id"=>"8", "priority"=>"LOW", "vendor_id"=>"1", "placeholder"=>"1", "due"=>"08-18-2015", "mandatory_due_date"=>"0", "is_budgetary"=>"0", "ship_date"=>"standard", "end_user_list"=>"", "application_list"=>"", "notes"=>""}, "weeks"=>"1", "commit"=>"Create Rfq"}
  [1m[36mUser Load (0.5ms)[0m  [1mSELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1[0m  [["id", 4]]
Unpermitted parameter: priority
  [1m[35mCustomer Load (0.1ms)[0m  SELECT  "customers".* FROM "customers" WHERE "customers"."id" = ? LIMIT 1  [["id", 8]]
  [1m[36m (0.2ms)[0m  [1mbegin transaction[0m
  [1m[35m (0.1ms)[0m  rollback transaction
  [1m[36mVendor Load (0.2ms)[0m  [1mSELECT "vendors".* FROM "vendors"[0m
  Rendered rfqs/_rfq_attachment_fields.html.slim (0.9ms)
  Rendered rfqs/_quote_fields.html.slim (0.7ms)
  [1m[35mVendor Load (0.1ms)[0m  SELECT  "vendors".* FROM "vendors" WHERE "vendors"."id" = ? LIMIT 1  [["id", 1]]
  [1m[36mFormAttribute Load (0.1ms)[0m  [1mSELECT "form_attributes".* FROM "form_attributes" WHERE "form_attributes"."vendor_id" = ? AND (attribute = 'productline')[0m  [["vendor_id", 1]]
  [1m[35mCACHE (0.0ms)[0m  SELECT  "vendors".* FROM "vendors" WHERE "vendors"."id" = ? LIMIT 1  [["id", 1]]
  [1m[36mFormAttribute Load (0.1ms)[0m  [1mSELECT "form_attributes".* FROM "form_attributes" WHERE "form_attributes"."vendor_id" = ? AND (attribute = 'discwedge')[0m  [["vendor_id", 1]]
  [1m[35mCACHE (0.0ms)[0m  SELECT  "vendors".* FROM "vendors" WHERE "vendors"."id" = ? LIMIT 1  [["id", 1]]
  [1m[36mFormAttribute Load (0.1ms)[0m  [1mSELECT "form_attributes".* FROM "form_attributes" WHERE "form_attributes"."vendor_id" = ? AND (attribute = 'valvetype')[0m  [["vendor_id", 1]]
  [1m[35mCACHE (0.0ms)[0m  SELECT  "vendors".* FROM "vendors" WHERE "vendors"."id" = ? LIMIT 1  [["id", 1]]
  [1m[36mFormAttribute Load (0.1ms)[0m  [1mSELECT "form_attributes".* FROM "form_attributes" WHERE "form_attributes"."vendor_id" = ? AND (attribute = 'valveends')[0m  [["vendor_id", 1]]
  [1m[35mCACHE (0.0ms)[0m  SELECT  "vendors".* FROM "vendors" WHERE "vendors"."id" = ? LIMIT 1  [["id", 1]]
  [1m[36mFormAttribute Load (0.1ms)[0m  [1mSELECT "form_attributes".* FROM "form_attributes" WHERE "form_attributes"."vendor_id" = ? AND (attribute = 'valvematerial')[0m  [["vendor_id", 1]]
  [1m[35mCACHE (0.0ms)[0m  SELECT  "vendors".* FROM "vendors" WHERE "vendors"."id" = ? LIMIT 1  [["id", 1]]
  [1m[36mFormAttribute Load (0.1ms)[0m  [1mSELECT "form_attributes".* FROM "form_attributes" WHERE "form_attributes"."vendor_id" = ? AND (attribute = 'valveconfig')[0m  [["vendor_id", 1]]
  [1m[35mCACHE (0.0ms)[0m  SELECT  "vendors".* FROM "vendors" WHERE "vendors"."id" = ? LIMIT 1  [["id", 1]]
  [1m[36mFormAttribute Load (0.1ms)[0m  [1mSELECT "form_attributes".* FROM "form_attributes" WHERE "form_attributes"."vendor_id" = ? AND (attribute = 'class')[0m  [["vendor_id", 1]]
  [1m[35mCACHE (0.0ms)[0m  SELECT  "vendors".* FROM "vendors" WHERE "vendors"."id" = ? LIMIT 1  [["id", 1]]
  [1m[36mFormAttribute Load (0.1ms)[0m  [1mSELECT "form_attributes".* FROM "form_attributes" WHERE "form_attributes"."vendor_id" = ? AND (attribute = 'operator')[0m  [["vendor_id", 1]]
  Rendered rfqs/_line_item_fields.html.slim (4.6ms)
  Rendered rfqs/_valve_fields_xanik.html.slim (19.3ms)
  Rendered rfqs/_form.html.slim (49.7ms)
  Rendered rfqs/new.html.slim within layouts/application (50.9ms)
Completed 200 OK in 1519ms (Views: 1496.4ms | ActiveRecord: 2.1ms)
编辑:意外修复了

所以我改变了

@rfq = Rfq.new(rfq_params)
@rfq.owner = current_user.first_name.titleize
if @rfq.due.present?
  @rfq.due = Date.strptime(rfq_params[:due].to_str,"%m-%d-%Y")
  @rfq.due = @rfq.due.change(hour: 15)
end

@rfq = Rfq.new(rfq_params)
@rfq.owner = current_user.first_name.titleize
if rfq_params[:due].present?
  @rfq.due = Date.strptime(rfq_params[:due].to_str,"%m-%d-%Y")
  @rfq.due = @rfq.due.change(hour: 15)
end

现在它有效,但我真的不明白为什么......

很抱歉,如果这很明显,我只是Rails的新手

0 个答案:

没有答案