Rails 4.2.6 validation只在console生效,controller不生效(from browser)

Rails 4.2.6 validation takes effect only in console, but not in controller (from browser)

已经完成了几个 SO rails 验证问题,但是 none 是合适的。

我的模特:

class InquiryInteger < Inquiry
    validates :answer, numericality: { only_integer: true, greater_than: 0 }
end

现在在 rails 控制台中,验证工作正常(意味着:当实例包含文本时不保存记录:

2.3.3 :017 > i1.save
   (0.2ms)  BEGIN
  Inquiry Exists (0.4ms)  SELECT  1 AS one FROM "inquiries" WHERE ("inquiries"."question_id" IS NULL AND "inquiries"."session_id" = 'jsklkjf8') LIMIT 1
   (0.2ms)  ROLLBACK
 => false
2.3.3 :018 > i1.errors.full_messages
 => ["Answer is not a number"]
2.3.3 :019 > i1.answer
 => "aADDASDASD"
2.3.3 :020 >

我的控制器创建动作:

...
respond_to do |format|
  if @inquiry.save
    format.html { redirect_to pages_path(url: params[:url]), notice: 'Thanks for your vote.' }
    format.json { render action: 'show', status: :created, location: @inquiry }
  else
    format.html { render action: 'new' }
    format.json { render json: @inquiry.errors, status: :unprocessable_entity }
  end
...

保存方法是让字符串通过并存储在数据库中。我重新启动了瘦网络服务器,所以一定已经加载了更改。即使在 byebug 中,保存方法也没有显示任何消息。

验证实例属性时必须设置正确的子类(显而易见)。 在控制台中,我总是使用 InquiryInteger,而在控制器中我没有正确设置子类型。

原来是Inquiry,因此验证失败。 正确设置实例类型后(例如InquiryIntegerInquiryText),验证终于生效了。