【问题标题】:Entered integer was not taken as a numeric in rails 3.1.0输入的整数在 rails 3.1.0 中没有被当作数字
【发布时间】:2012-02-02 02:12:09
【问题描述】:

在报价控制器中保存新报价时,我们的 rails 3.1.0 应用程序出现了一个奇怪的错误。报价属于 rfq 并且 rfq 是 18 被传递到报价控制器。错误是:

Validation failed: Quoted total is not a number

显示的参数有:

{"utf8"=>"✓",
 "authenticity_token"=>"p9waUq7wNk6djm9uRtDOA0eHLzzlJuSCWrTsSEBvcck=",
 "quote"=>{"test_item_ids"=>["1",
 "2"],
 "_destroy"=>"false",
 "quoted_total"=>"290"},
 "commit"=>"Save",
 "rfq_id"=>"18"}

正如它所显示的,quoted_total 是 290。但不知何故,它没有被识别为一个数字。

这是引号控制器中的创建代码:

    @quote = @rfq.quotes.new(params[:quote], :as => :roles_new)
    @quote.input_by_id = session[:user_id]
    @quote.test_items =  TestItem.find_all_by_id(params[:quote][:test_item_ids]) 
    if @quote.save!
      redirect_to URI.escape("/view_handler?index=0&msg=quote saved!")
    else
      flash.now[:error] = "Not saved!"
      render 'new'
    end

错误是由@quote.save 引起的。报价控制器的 rspec 代码具有相同的错误。是什么导致了错误?非常感谢。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.1 rspec2


    【解决方案1】:

    我猜你在你的模型中验证了quoted_total 作为一个数字,事实上,params[:quoted_total] 是一个字符串。

    【讨论】:

    • 是的,如:validates_numericality_of :quoted_total, :greater_than => 0.00。但是params返回的所有数据都是字符串,模型知道如何转换它们。
    • 发现问题了!这是由 attr_accessible 中的错误名称引起的。 quoted_total 不在 attr_accessible 中,因此无法传入其值。
    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 2015-08-29
    • 2013-07-28
    • 2016-12-06
    • 2012-08-01
    • 1970-01-01
    相关资源
    最近更新 更多